# The Java Collections Framework Hierarchy

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

Canonical: https://www.internhack.in/learn/interview/java-interview/java-collections-hierarchy
Last updated: 2026-11-28

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

- Map is not a child of Collection.
- Collection is the interface; Collections is a utility class.
- Default choices: ArrayList, HashMap, HashSet, ArrayDeque.
- TreeMap and TreeSet give sorted order at O(log n).
- List.of, Set.of and Map.of create immutable collections.

## Common follow-up questions

- Why does Map not extend Collection?
- What is the difference between Collection and Collections?
- When would you use LinkedHashMap?
- How is PriorityQueue implemented internally?

## Interview tips

- Start with the tree structure aloud: Iterable, Collection, List/Set/Queue, and Map separate.
- Attach a time complexity to every structure you name; it shows you understand the trade-offs.

## Concepts covered

Collection, List, Set, Queue, Map

Previous: [try-with-resources and AutoCloseable](https://www.internhack.in/learn/interview/java-interview/java-try-with-resources) · Next: [ArrayList vs LinkedList](https://www.internhack.in/learn/interview/java-interview/java-arraylist-vs-linkedlist) · [All of Core Java & Collections](https://www.internhack.in/learn/interview/java-interview)

## More Core Java & Collections questions

- [How HashMap Works Internally](https://www.internhack.in/learn/interview/java-interview/java-hashmap-internals)
- [HashMap vs Hashtable vs ConcurrentHashMap](https://www.internhack.in/learn/interview/java-interview/java-hashmap-hashtable-concurrenthashmap)
- [How HashSet Works Internally](https://www.internhack.in/learn/interview/java-interview/java-hashset-internals)
- [Comparable vs Comparator and How TreeMap Sorts](https://www.internhack.in/learn/interview/java-interview/java-comparable-comparator-treemap)
- [Fail-Fast vs Fail-Safe Iterators](https://www.internhack.in/learn/interview/java-interview/java-fail-fast-vs-fail-safe)
- [Generics, Wildcards and Type Erasure](https://www.internhack.in/learn/interview/java-interview/java-generics-type-erasure)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [Core Java & Collections](https://www.internhack.in/learn/interview/java-interview)
