Detailed explanation
The Java Collections Framework is a set of interfaces, implementations, and algorithms for storing and manipulating groups of objects. Its main collection hierarchy starts with Iterable, which provides iterator() and enables enhanced for loops. Collection extends Iterable and defines common operations such as add(), remove(), size(), and contains(), although modification operations may be unsupported. List, Set, and Queue are its principal subinterfaces. Map belongs to the same framework but is separate from Collection because it represents key-value associations rather than individual elements.
List represents an ordered sequence, supports index-based access, and permits duplicates. ArrayList uses a resizable array, giving constant-time indexed access and amortized constant-time appends; insertion or removal in the middle usually shifts elements. LinkedList uses doubly linked nodes and implements both List and Deque. Set disallows duplicate elements. HashSet uses a backing HashMap and makes no iteration-order guarantee. LinkedHashSet maintains a defined encounter order, normally insertion order. TreeSet implements NavigableSet, which extends SortedSet, and stores elements in sorted order using natural ordering or a Comparator. Its basic operations typically take logarithmic time.
Queue models elements awaiting processing; its ordering depends on the implementation, so queues are not universally FIFO. PriorityQueue exposes the least element according to its ordering at the head, but its iterator does not produce sorted output. Deque extends Queue and supports insertion and removal at both ends. ArrayDeque is a common implementation for stacks and FIFO queues; LinkedList also implements it. Map implementations include HashMap, LinkedHashMap, TreeMap, and ConcurrentHashMap. HashMap uses hashing, LinkedHashMap provides insertion order or configurable access order, TreeMap implements NavigableMap for sorted keys, and ConcurrentHashMap supports concurrent access.
In interviews, distinguish interfaces from concrete classes and connect each choice to ordering, duplicates, lookup cost, and concurrency. For example, List<String> list = new ArrayList<>(); declares the required contract while selecting a specific implementation. Likewise, Set<String> set = new TreeSet<>(); selects sorted uniqueness, while Map<String, Integer> map = new HashMap<>(); selects key-value lookup without guaranteed iteration order. This is the core hierarchy; modern Java also adds SequencedCollection, SequencedSet, and SequencedMap for uniform encounter-order operations.
Key points to remember
- Iterable → Collection → List, Set, and Queue; Deque extends Queue.
- List permits duplicates and indexed access; Set enforces uniqueness.
- Queue ordering is implementation-specific; Deque supports operations at both ends.
- Map is separate from Collection; SortedMap → NavigableMap describes its sorted-key branch.
- Choose implementations by ordering, operation costs, null handling, and thread-safety requirements.
Common mistakes candidates make
- Placing Map under Collection or confusing the Collection interface with the Collections utility class.
- Calling Deque a concrete implementation, or forgetting that LinkedList implements both List and Deque.
- Assuming every queue is FIFO, HashSet preserves insertion order, or PriorityQueue iteration is sorted.
Likely follow-up questions
What is the difference between Collection and Collections?
Collection is an interface representing a group of elements. Collections is a utility class containing static methods such as sort(), reverse(), and unmodifiableList().
How do HashSet and TreeSet determine duplicates?
HashSet relies on hashCode() and equals(), which must obey their contracts. TreeSet treats elements as duplicates when their comparison returns zero, so its ordering should be consistent with equals() to satisfy the general Set contract.
Why declare List<String> instead of ArrayList<String>?
Declaring the interface keeps client code dependent on the required contract rather than a particular implementation. You can change implementations more easily, provided their behaviour and performance still meet the application's requirements.
Interview tip: Sketch the interface hierarchy first, then name representative implementations and explain one practical trade-off for each branch.
