Key Java Collections Implementations: List, Set, Map
Fast recall
Quick revision
Core answer
ArrayList (resizable array, fast random access) and LinkedList (doubly linked, fast at the ends). Set: HashSet (unordered, expected O(1)), LinkedHashSet (insertion order), TreeSet (sorted, O(log n)). Map: HashMap (unordered, expected O(1)), LinkedHashMap (insertion/access order), TreeMap (sorted by key, O(log n)).Breadth across all three sub-hierarchies and awareness of the ordering/sorting/speed trade-offs
Only knowing ArrayList and HashMap; forgetting LinkedHashMap or TreeMap and when each fits
Give the one-line use case for each and warn that LinkedList.get(i) in a loop is O(n^2)
Interview-ready guide
Interview answer
For List, the two main implementations are ArrayList and LinkedList.
ArrayList uses a resizable array internally, so it's great for random access — calling get with an index is constant time. LinkedList uses a doubly linked structure, so it's better when you frequently insert or remove at the beginning or end, and it also implements Deque so you can treat it like a stack or queue. Use ArrayList for indexed access; use LinkedList for queue-like work.
For Set you have three choices. HashSet is backed by a hash table, so it's unordered but very fast — add and contains are constant time on average. LinkedHashSet is like HashSet but preserves insertion order.
TreeSet uses a red-black tree, so it keeps elements sorted, with log-n operations. Use HashSet for speed, LinkedHashSet when order matters, and TreeSet when you need sorted iteration. For Map it's the same pattern.
HashMap is a hash table — unordered, but get and put are constant time on average. LinkedHashMap preserves insertion or access order, which makes it perfect for building an LRU cache. TreeMap is a red-black tree, so keys are sorted and you get range queries like headMap and tailMap, at log-n cost.
The way to choose is to ask three questions: do I need fast lookup, insertion order, or sorted order? Two big mistakes to avoid: don't only ever reach for ArrayList and HashMap, and never call get with an index in a loop on a LinkedList, because that's quadratic time. Start with ArrayList, HashSet, and HashMap, then switch to the ordered or sorted variants only when your use case actually needs them.
Key Java Collections Implementations: List, Set, Map
Direct Answer
List: ArrayList (resizable array, fast random access) and LinkedList (doubly linked, fast at the ends). Set: HashSet (unordered, expected O(1)), LinkedHashSet (insertion order), TreeSet (sorted, O(log n)). Map: HashMap (unordered, expected O(1)), LinkedHashMap (insertion/access order), TreeMap (sorted by key, O(log n)).
Code Example
import java.util.*;
public class PickImpl {
public static void main(String[] args) {
// List: indexed access -> ArrayList
List<String> products = new ArrayList<>();
products.add("P-1");
String p = products.get(0); // O(1)
// List as a queue/deque -> LinkedList
Deque<String> tasks = new LinkedList<>();
tasks.addFirst("t1"); tasks.addLast("t2");
// Set: fast membership, no order -> HashSet
Set<String> seen = new HashSet<>();
seen.add("u1"); boolean has = seen.contains("u1"); // O(1)
// Set: sorted iteration -> TreeSet
Set<String> sorted = new TreeSet<>(List.of("c", "a", "b")); // [a, b, c]
// Map: fast lookup by id -> HashMap
Map<String, Integer> price = new HashMap<>();
price.put("P-1", 100);
// Map: keep insertion order -> LinkedHashMap
Map<String, Integer> ordered = new LinkedHashMap<>();
// Map: sorted keys / range queries -> TreeMap
TreeMap<Integer, String> byId = new TreeMap<>();
byId.put(3, "c"); byId.put(1, "a");
SortedMap<Integer, String> firstTwo = byId.headMap(3); // range query
}
}
Common Mistakes & Pitfalls
- Only knowing ArrayList and HashMap. Defaulting to these for everything misses
LinkedList,TreeMap, andLinkedHashMapuse cases. LinkedList.get(i)in a loop. Random access on a linked list is O(n), so an indexed loop becomes O(n^2). UseArrayListfor indexed access.- Not knowing LinkedHashMap. It backs LRU caches via access-order mode and
removeEldestEntry— a common follow-up. - Using TreeSet/TreeMap when order doesn't matter. You pay O(log n) for sorting you don't need;
HashSet/HashMapare expected O(1).
Check your understanding
Answer these without looking back at the article.
- When would you choose TreeSet over HashSet?
- What is LinkedHashMap used for in practice?
- What is the time complexity of HashMap get and put?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.