The Java Collections Framework Hierarchy

Difficulty: Beginner

Question

Explain the Java Collections hierarchy. What are the main interfaces, their implementations, and how do you choose between them?

Answer

The Collections Framework is the toolbox you will use in every single Java project, so interviewers expect you to have the mental map in your head. Let me draw it in words and then show you how to pick.

At the root is the Iterable interface, which lets you use the for-each loop. Below it is Collection, representing a group of elements. Three main interfaces extend Collection: List, Set and Queue. Map is a separate hierarchy; it does not extend Collection because it stores key-value pairs rather than single elements. That last point is a very popular trick question.

List is an ordered collection that allows duplicates and access by index. The main implementations are ArrayList (backed by a resizable array), LinkedList (doubly linked list that also implements Deque), Vector (legacy synchronized array) and its subclass Stack (also legacy). In modern code you use ArrayList by default.

Set holds unique elements. HashSet uses hashing and gives constant time operations with no order guarantee. LinkedHashSet remembers insertion order. TreeSet keeps elements sorted using a red-black tree, giving O(log n) operations, and implements NavigableSet with methods like floor, ceiling and headSet. Both HashSet and LinkedHashSet allow one null; TreeSet does not with natural ordering.

Queue models elements waiting to be processed, usually first-in first-out. PriorityQueue is a binary heap that always hands out the smallest element (or per a comparator) first. Deque, a double-ended queue, is implemented by ArrayDeque, which is the recommended choice for both stack and queue behaviour, faster than Stack and LinkedList in most cases.

Map implementations mirror the sets. HashMap is the general-purpose choice. LinkedHashMap keeps insertion order, or access order if configured, which makes it ideal for building an LRU cache. TreeMap keeps keys sorted. Hashtable is legacy and synchronized. ConcurrentHashMap is the modern thread-safe map. EnumMap and IdentityHashMap serve niche cases.

Do not confuse Collection with Collections. Collections (plural) is a utility class with static methods such as sort, reverse, shuffle, unmodifiableList, synchronizedList and binarySearch.

How to choose? Ask three questions. Do I need ordering or sorting? Do I need uniqueness? Do I need lookup by key? For index-based access and iteration, pick ArrayList. For fast membership testing on unique items, HashSet. For key-based lookup, HashMap. For sorted output, TreeSet or TreeMap. For FIFO processing, ArrayDeque. For priority scheduling, PriorityQueue. For thread-safe maps, ConcurrentHashMap.

Also know the immutable factory methods added in Java 9: List.of, Set.of and Map.of. They return unmodifiable collections that reject nulls, and calling add on them throws UnsupportedOperationException. Set.of and Map.of also have unspecified iteration order and throw on duplicates.

Time complexity is a common follow-up. ArrayList get is O(1) and add at end amortized O(1); HashMap and HashSet operations are O(1) on average; TreeMap and TreeSet are O(log n); PriorityQueue offer and poll are O(log n) while peek is O(1).

One more practical remark: program to the interface. Declare variables as List or Map and let the implementation be a detail, so you can swap it later without touching callers.

Code examples

Picking the right collection

import java.util.*;

public class CollectionsTour {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>(List.of(3, 1, 3));
        Set<Integer> set = new HashSet<>(list);
        Deque<Integer> stack = new ArrayDeque<>();
        stack.push(1);
        stack.push(2);
        PriorityQueue<Integer> pq = new PriorityQueue<>(List.of(5, 1, 3));
        Map<String, Integer> sorted = new TreeMap<>();
        sorted.put("pear", 2);
        sorted.put("apple", 1);

        System.out.println(list);
        System.out.println(set);
        System.out.println(stack.pop());
        System.out.println(pq.poll());
        System.out.println(sorted);
    }
}

The list keeps duplicates and order, the set removes duplicates, the deque behaves as a stack (last pushed comes out first), the priority queue yields the smallest element and the TreeMap sorts by key.

Key points

Common follow-up questions

Interview tips

Concepts covered

Collection, List, Set, Queue, Map

More Core Java & Collections questions