List vs Set vs Map — When to Use Each Collection Type
Fast recall
Quick revision
Core answer
ArrayList, LinkedList) is an ordered, indexed sequence allowing duplicates — use when position matters or duplicates are valid (e.g., a queue of orders, a sequence of events). Set (HashSet, LinkedHashSet, TreeSet) is a collection of unique elements — use when uniqueness is a correctness requirement (e.g., a set of user IDs, tags). Map (HashMap, LinkedHashMap, TreeMap) is a collection of key-value pairs with unique keys — use when you need fast lookup by key (e.g., user-by-ID, config-by-key). Picking the wrong interface masks bugs: using List instead of Set allows silent duplicates; using Map instead of Set wastes memory on null values. Common variants: ordered (LinkedHashSet, LinkedHashMap) for insertion-order iteration; sorted (TreeSet, TreeMap) for natural-order iteration and range queries; concurrent (CopyOnWriteArrayList, ConcurrentHashMap) for thread-safe access. For interviews: pick the interface based on intent first, then choose the implementation based on performance characteristics. Don't default to ArrayList for everything.Understanding of the fundamental collection interfaces and when each is appropriate
Using List when Set is more appropriate (allowing silent duplicates), or using Map when a Set would suffice
Mention that the interface choice drives the correctness guarantees — Set enforces uniqueness, Map enforces key uniqueness — and that picking the wrong interface masks bugs
Interview-ready guide
Interview answer
Java's three core collection interfaces — List, Set, and Map — each enforce a different invariant about the data they hold: ordering with duplicates allowed, uniqueness with no ordering guarantee, and unique-key-to-value association. Choosing the right interface is about declaring what constraint the data must satisfy, not just what container to use.
List — ordered, allows duplicates:
- Use when elements can repeat and sequence or index matters
- Examples: query results, ordered log entries, ranked items
Set — unique elements only:
- Enforces uniqueness automatically — no manual duplicate checking needed
- Use when duplicates are invalid; the interface choice is a correctness guarantee, not just a convenience
- Mistake: using a List when Set is appropriate — duplicates accumulate silently, bugs surface far from the cause
Map — key-value associations:
- Each key is unique and maps to exactly one value
- If you only care about key existence without values, use a Set — Map with dummy values wastes memory and obscures intent
I recommend always choosing the most restrictive interface that satisfies requirements — it makes invalid states unrepresentable at the type level.
List vs Set vs Map — When to Use Each Collection Type
Direct Answer
List, Set, and Map are the three core collection interfaces in Java, and the choice between them encodes intent: List (ArrayList, LinkedList) is an ordered, indexed sequence allowing duplicates — use when position matters or duplicates are valid (e.g., a queue of orders, a sequence of events). Set (HashSet, LinkedHashSet, TreeSet) is a collection of unique elements — use when uniqueness is a correctness requirement (e.g., a set of user IDs, tags). Map (HashMap, LinkedHashMap, TreeMap) is a collection of key-value pairs with unique keys — use when you need fast lookup by key (e.g., user-by-ID, config-by-key). Picking the wrong interface masks bugs: using List instead of Set allows silent duplicates; using Map instead of Set wastes memory on null values. Common variants: ordered (LinkedHashSet, LinkedHashMap) for insertion-order iteration; sorted (TreeSet, TreeMap) for natural-order iteration and range queries; concurrent (CopyOnWriteArrayList, ConcurrentHashMap) for thread-safe access. For interviews: pick the interface based on intent first, then choose the implementation based on performance characteristics. Don't default to ArrayList for everything.
Deep Dive & Architecture
compute, merge, computeIfAbsent, and computeIfPresent are Map methods (Java 8+) that atomically combine get + check + put into a single operation, replacing the verbose get-check-put anti-pattern. computeIfAbsent(key, mappingFunction) — if key absent, compute value and put; most-used for lazy initialization of multi-valued maps: map.computeIfAbsent(key, k -> new ArrayList<>()).add(value). computeIfPresent(key, remappingFunction) — if key present, recompute value (or remove if null returned). compute(key, remappingFunction) — recompute regardless of presence (function gets old value or null). merge(key, value, remappingFunction) — if absent, put value; if present, apply function to combine: map.merge(word, 1, Integer::sum) is the idiomatic word-counter. Why they matter: (1) atomic in ConcurrentHashMap — no lost updates from concurrent get-check-put races; (2) less code than if (map.containsKey(k)) map.put(k, ...) else map.put(k, ...); (3) cleaner intent — the operation says what it does. Pitfalls: (1) not atomic in non-concurrent maps (HashMap) — only the API is single-call, not thread-safe; (2) returning null from a remapping function removes the key — surprising; (3) don't recursively modify the map inside the function — undefined behavior; (4) merge doesn't handle null values the same way as compute — read the Javadoc. For interviews: lead with 'replaces get-check-put, atomic in ConcurrentHashMap, idiomatic for multimap lazy-init and counters'.
List, Set, and Map are the three core collection interfaces in Java, and the choice between them encodes intent: List (ArrayList, LinkedList) is an ordered, indexed sequence allowing duplicates — use when position matters or duplicates are valid (e.g., a queue of orders, a sequence of events). Set (HashSet, LinkedHashSet, TreeSet) is a collection of unique elements — use when uniqueness is a correctness requirement (e.g., a set of user IDs, tags). Map (HashMap, LinkedHashMap, TreeMap) is a collection of key-value pairs with unique keys — use when you need fast lookup by key (e.g., user-by-ID, config-by-key). Picking the wrong interface masks bugs: using List instead of Set allows silent duplicates; using Map instead of Set wastes memory on null values. Common variants: ordered (LinkedHashSet, LinkedHashMap) for insertion-order iteration; sorted (TreeSet, TreeMap) for natural-order iteration and range queries; concurrent (CopyOnWriteArrayList, ConcurrentHashMap) for thread-safe access. For interviews: pick the interface based on intent first, then choose the implementation based on performance characteristics.
A common mistake is confusing the interface with the implementation (e.g., saying List when you mean ArrayList), or not knowing that Map does not extend Collection.
Common Mistakes & Pitfalls
Using List when Set is more appropriate (allowing silent duplicates), or using Map when a Set would suffice
Check your understanding
Answer these without looking back at the article.
- What is the Java Collections Framework (JCF) and why does it exist? It provides a unified architecture of interfaces and implementations for storing and manipulating groups of objects, replacing the ad-hoc pre-JCF classes (Vector, Hashtable, Enumeration) with a consistent, interoperable API.
- How does HashSet work internally in Java?
- How does HashMap internally handle hash collisions in Java 8+? Explain the transition from linked list to tree.
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.