Collections
30 interview questions and answers in the Collections section.
Questions in this section
- What are the main Collection Framework interfaces?
- What is the difference between List, Set and Queue?
- What is the difference between ArrayList and LinkedList?
- When to use ArrayList vs LinkedList?
- What is the time complexity of operations in ArrayList?
- What is the time complexity of operations in LinkedList?
- What is Vector and how does it differ from ArrayList?
- What is Stack?
- What is Queue and what implementations exist?
- What is Deque?
- What is the difference between HashSet, LinkedHashSet and TreeSet?
- How does HashSet work internally?
- What is TreeSet and how does it work?
- What is Map and what implementations exist?
- What is the difference between HashMap, LinkedHashMap and TreeMap?
- When to use TreeMap?
- What is WeakHashMap?
- What is ConcurrentHashMap?
- How does ConcurrentHashMap ensure thread-safety?
- What is CopyOnWriteArrayList?
- When to use synchronized collections?
- How to get a synchronized collection?
- What is Collections.unmodifiableList()?
- How does Collections.unmodifiableList() work internally?
- What is the difference between Iterator and ListIterator?
- What are fail-fast and fail-safe iterators?
- What is ConcurrentModificationException?
- How to properly remove elements during iteration?
- What is Comparable and Comparator?
- What operations does the Collection interface support?
Study navigator
30 questions for Middle Java Developer interview preparation.
π All Questions
πΊοΈ Topic Dependency Map
ββββββββββββββββββββββββββββββββββββββββββββ
β 1. Collection Framework (Overview) β
β Collection, List, Set, Queue, Map β
ββββ¬βββββββββββ¬βββββββββββ¬ββββββββββββββββββ
β β β
ββββββββββββ β ββββββββββββ
βΌ βΌ βΌ
βββββββββββββββββ βββββββββββββββββ ββββββββββββββββββββββ
β LIST (3-10) β β SET (11-13) β β MAP (14-20) β
β 3. ArrayList β β 11. HashSet β β 14. Map overview β
β vs Linked β β vs Linked β β 15. HashMap vs β
β 4. When what β β vs Tree β β LinkedHashMap β
β 5-6. Big O β β 12. HashSet β β vs TreeMap β
β 7. Vector β β internals β β 16. When TreeMap β
β 8. Stack β β 13. TreeSet β β 17. WeakHashMap β
β 9. Queue β β β β 18-19. CHM β
β 10. Deque β β β β 20. CopyOnWrite β
βββββββββ¬ββββββββ βββββββββ¬ββββββββ ββββββββββ¬ββββββββββββ
β β β
ββββββββββββββββββββββΌββββββββββββββββββββββ
βΌ
ββββββββββββββββββββββββββββββββββββββββββββ
β THREAD-SAFETY (18-22) β
β 18-19. ConcurrentHashMap β
β 20. CopyOnWriteArrayList β
β 21-22. synchronized collections β
ββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββ
β ITERATORS and REMOVAL (23-28) β
β 23-24. unmodifiableList β
β 25. Iterator vs ListIterator β
β 26. fail-fast vs fail-safe β
β 27. ConcurrentModificationException β
β 28. Proper removal β
ββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββ
β COMPARISON and COMMON OPS (29, 31) β
β 29. Comparable vs Comparator β
β 31. Collection operations β
ββββββββββββββββββββββββββββββββββββββββββββ
π― Recommended Study Order
π’ Junior Level (weeks 1-2)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Collection Framework Overview | Q1, Q2 | Interface hierarchy, List vs Set vs Queue |
| 2 | List: ArrayList vs LinkedList | Q3, Q4 | When to use what |
| 3 | Vector, Stack, Queue, Deque | Q7, Q8, Q9, Q10 | Legacy and modern analogs |
| 4 | Set: HashSet vs TreeSet | Q11, Q13 | Uniqueness and sorting |
| 5 | Map: HashMap vs TreeMap | Q14, Q15, Q16 | When to use what |
| 6 | unmodifiableList | Q23, Q24 | Defensive copying |
| 7 | Iterator vs ListIterator | Q25 | Iteration and removal |
π‘ Middle Level (weeks 3-4)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Big O ArrayList | Q5 | Amortization, resize, System.arraycopy |
| 2 | Big O LinkedList | Q6 | Cache miss, GC pressure |
| 3 | HashSet internals | Q12 | hashCode, equals, treeification |
| 4 | WeakHashMap | Q17 | WeakReference, GC, ReferenceQueue |
| 5 | synchronized collections | Q21, Q22 | Decorator, mutex, check-then-act |
| 6 | fail-fast/fail-safe | Q26 | modCount, CME, snapshot |
| 7 | CME | Q27 | Causes, subList trap, views |
| 8 | Removal during iteration | Q28 | removeIf, Iterator.remove |
| 9 | Comparable vs Comparator | Q29 | Timsort, overflow, transitivity |
| 10 | Collection operations | Q31 | bulk ops, parallelStream, Spliterator |
π΄ Senior Level (weeks 5-6)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | ArrayList internals | Q5 (Senior) | SIMD, branch prediction, CPU pipeline |
| 2 | LinkedList internals | Q6 (Senior) | Cache locality catastrophe, pipeline stall |
| 3 | CHM thread-safety | Q18, Q19 | CAS, volatile, synchronized, LongAdder |
| 4 | CopyOnWriteArrayList | Q20 | Fail-safe, stale data, eventual consistency |
| 5 | HashSet internals | Q12 (Senior) | Treeification, transient, hash collisions |
| 6 | TreeMap internals | Q13, Q16 | Red-black tree, rebalancing, Skip List |
| 7 | unmodifiableList internals | Q24 | Defensive copy, RandomAccess, GC pressure |
| 8 | Concurrent collections | Q21 (Senior) | High contention, compound operations |
π Key Connections Between Topics
Topic: List
Q3 (ArrayList vs LinkedList) β Q4 (When to use what) β Q5-Q6 (Big O)
β
Q7 (Vector) β Q8 (Stack) β Q9 (Queue) β Q10 (Deque)
Key connections:
- Q3 β Q5-Q6: Big O explains why ArrayList is faster than LinkedList in practice
- Q4 β Q28: removeIf() solves the removal-in-loop problem
- Q7 β Q18: Vector β ConcurrentHashMap (evolution of thread-safety)
- Q9-Q10 β Q18: BlockingQueue, ConcurrentLinkedQueue as alternatives
Topic: Set
Q11 (HashSet vs LinkedHashSet vs TreeSet) β Q12 (HashSet internals) β Q13 (TreeSet)
Key connections:
- Q11 β Q14: Set uses Map internally (HashSet = HashMap<E, Object>)
- Q12 β Q15: HashSet internals = HashMap internals
- Q13 β Q16: TreeSet = TreeMap.keySet(), when TreeMap is needed
Topic: Map
Q14 (Map overview) β Q15 (HashMap vs LinkedHashMap vs TreeMap) β Q16 (TreeMap)
β β β
Q17 (WeakHashMap) Q12 (HashSet internals) Q18-19 (ConcurrentHashMap)
β
Q20 (CopyOnWriteArrayList)
Key connections:
- Q14 β Q1: Map is technically NOT a Collection, but part of the Framework
- Q15 β Q12: HashMap internals = HashSet internals
- Q17 β Q3 (Memory and GC): WeakReference, GC, SoftReference
- Q18-19 β Q21: CHM vs synchronizedList β different approaches to thread-safety
- Q18 β Q20: CHM for mixed workload, COWAL for read-heavy
Topic: Thread-Safety
Q7 (Vector) β Q21 (synchronized collections) β Q22 (How to get)
β β β
Q18 (CHM) βββββββββββββββ Q19 (CHM thread-safety) Q20 (CopyOnWriteArrayList)
Key connections:
- Q7 β Q21: Vector β legacy approach, synchronizedList β modern
- Q18 β Q20: CHM = granular locks, COWAL = snapshot
- Q21 β Q26: synchronizedList = fail-fast, CHM = weakly consistent
Topic: Iterators and removal
Q25 (Iterator vs ListIterator) β Q26 (fail-fast vs fail-safe)
β β
Q27 (CME) βββββββββββββββββββββββββ Q28 (Proper removal)
β
Q23-24 (unmodifiableList)
Key connections:
- Q25 β Q28: Iterator.remove() β the correct way to remove
- Q26 β Q27: fail-fast throws CME, fail-safe β does not
- Q23-24 β Q27: unmodifiableList does not protect against CME through view
π Cheat Sheet: What to Know for Each Level
π’ Junior
- Hierarchy: Collection β List/Set/Queue, Map β in parallel
- ArrayList vs LinkedList: ArrayList for most cases
- HashSet = uniqueness, TreeSet = sorting
- HashMap = key-value, TreeMap = sorted by key
- Vector/Stack β legacy, use ArrayList/ArrayDeque
- Iterator for iteration, cannot remove in for-each
- unmodifiableList = defensive wrapper, not deep immutability
π‘ Middle
- Big O: ArrayList add = amort. O(1), get = O(1); LinkedList get = O(n)
- Cache locality: ArrayList > LinkedList 10-100x on iteration
- HashSet = HashMap internally, treeification at > 8 elements per bucket
- ConcurrentHashMap: CAS + volatile + synchronized, does NOT block reads
- CopyOnWriteArrayList: snapshot, fail-safe, O(n) copy on write
- fail-fast = modCount, CME = bug detector (not synchronization!)
- removeIf() = O(n) for ArrayList, Iterator.remove() = O(nΒ²)
- Comparable = natural order, Comparator = external sorting
π΄ Senior
- System.arraycopy: JVM intrinsic, SIMD, branch prediction
- LinkedList cache miss: each node β a separate object on the heap
- CHM: CounterCells (LongAdder), ForwardingNode, ReservationNode
- WeakHashMap: ReferenceQueue, expungeStaleEntries, Minor GC
- ConcurrentHashMap size(): not a lock, but a counter via CAS
- Timsort: stable sorting, transitivity, overflow
- Spliterator: characteristics (SIZED, DISTINCT, SORTED) for Stream optimization
- Java 21: SequencedCollection/SequencedSet/SequencedMap
π Format of Each File
Each file contains:
- π’ Junior Level β basic understanding, simple analogies, examples
- π‘ Middle Level β internals, typical mistakes, practical examples
- π΄ Senior Level β deep dive, edge cases, production experience, monitoring
- π― Interview Cheat Sheet β key points, frequent questions, red flags, related topics