Skip to content
Preview · sample content · Explore all pages

Java Collections

Choose a collection based on the behavior you need.

Java collections expose operations such as positional access, membership, key lookup, and endpoint removal; their implementations have different traversal and storage costs.

Choose the question first

Use a List for ordered values, a Set for membership, a Map for a key-to-value association, or a Deque for endpoint work. Map is part of the collections framework but is not a subtype of Collection.

Ordering is a contract

TreeMap orders keys. LinkedHashMap keeps encounter order. PriorityQueue orders removals but does not supply sorted iteration. Those guarantees answer different questions.

Common Mistakes

Do not choose LinkedList simply because the workload inserts elements: finding an insertion position can dominate the edit. Count traversal, copying, allocation, and synchronisation needs before selecting an implementation.

Curriculum

Choose the operation family and keep mutation within a defined traversal policy.

  1. 1List, set or map?
  2. 2Java Iterator and ListIterator: traversal and controlled edits

Store mappings or unique values under a stable equality contract.

  1. 1Java HashMap: keys, collisions, and update operations
  2. 2Java HashSet: deduplication and equality
Storage details