Collections Hierarchy: List, Set, Queue and Map
The Java Collections Framework gives you List, Set, Queue and Map with several implementations each. Choosing the right one by ordering, duplicates and time complexity is a basic Java interview skill.
What you will learn
- Draw the main interfaces and implementations
- Know the time complexity of common operations
- Pick the right collection for a requirement
- Create immutable collections
Key concepts
List
Ordered, allows duplicates, index access. ArrayList (default), LinkedList.
Set
No duplicates. HashSet (no order, O(1)), LinkedHashSet (insertion order), TreeSet (sorted, O(log n)).
Queue / Deque
ArrayDeque for stacks and queues (faster than Stack and LinkedList), PriorityQueue for a heap ordered by priority.
Map
Key → value, not a Collection. HashMap, LinkedHashMap (insertion or access order, good for LRU caches), TreeMap (sorted keys, floorKey and ceilingKey).
Step by step
Complexity cheat sheet
get/contains add remove
ArrayList O(1) by index O(1)* O(n)
LinkedList O(n) O(1) at ends O(1) at ends
HashSet/HashMap O(1) avg O(1) avg O(1) avg
TreeSet/TreeMap O(log n) O(log n) O(log n)
PriorityQueue peek O(1) O(log n) poll O(log n)
* amortizedExamples
An LRU cache with LinkedHashMap
class LruCache<K, V> extends LinkedHashMap<K, V> {
private final int capacity;
LruCache(int capacity) {
super(16, 0.75f, true); // accessOrder = true
this.capacity = capacity;
}
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > capacity;
}
}
List<String> fixed = List.of("a", "b"); // immutable, no nulls
List<String> copy = List.copyOf(mutable);Practise it
Try these without looking back. If you can explain each one out loud, you are ready for this question.
- Pick a collection for: unique visitor ids, top 10 scores, a task queue, words in sorted order.
- Explain why Map does not extend Collection.
Interview questions
What interviewers ask next on this topic. Answer out loud first, then open the model answer.
Collection vs Collections?
Collection is the root interface for List, Set and Queue. Collections is a utility class with static helpers such as sort, unmodifiableList, synchronizedList and emptyList.
List.of vs Arrays.asList vs Collections.unmodifiableList?
List.of is truly immutable and rejects nulls. Arrays.asList is fixed-size but set() works and writes through to the array. unmodifiableList is a read-only view: changes to the underlying list still show through.
Summary
List for ordered duplicates, Set for uniqueness, Queue/Deque for FIFO and stacks, Map for key lookup; pick hash, linked or tree versions by ordering and complexity needs.
Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus