The Java Collections Framework: Internal Architecture

How Java collections work inside: the interface hierarchy and sequenced collections (Java 21), ArrayList vs LinkedList, HashMap internals (hashing, buckets, treeification, resizing), LinkedHashMap and LRU caches, TreeMap, sets, queues and deques, immutable collections, fail-fast iterators, and choosing the right collection.

On this page

Simple definition#

The Java Collections Framework (JCF) is a set of interfaces (List, Set, Map, Queue, Deque), implementations (ArrayList, HashMap, TreeMap and others) and algorithms (Collections.sort, binarySearch) for storing and processing groups of objects.

The problem#

Choosing the wrong collection is one of the most common hidden performance bugs: contains on a large ArrayList is O(n), and LinkedList.get(i) walks the list. Mutable keys break HashMap. Knowing the internals tells you the costs.

The hierarchy#

Core collection interfaces (Java 21)
flowchart TB
    IT["Iterable"] --> COL["Collection"]
    COL --> SC["SequencedCollection (21)"]
    SC --> LIST["List"]
    SC --> DQ["Deque"]
    SC --> SS["SequencedSet (21)"]
    COL --> SET["Set"] --> SS
    SS --> SOS["SortedSet → NavigableSet"]
    COL --> Q["Queue"] --> DQ
    MAP["Map (not a Collection)"] --> SM["SequencedMap (21)"] --> SOM["SortedMap → NavigableMap"]

Sequenced collections (JEP 431, Java 21) add a uniform API for collections with a defined encounter order: getFirst(), getLast(), addFirst(), addLast(), removeFirst(), removeLast() and reversed(). Maps get firstEntry(), pollLastEntry(), putFirst() and sequencedKeySet().

Lists#

ArrayListLinkedList
StructureResizable array (grows by about 1.5×)Doubly linked nodes
get(i)O(1)O(n)
Add at endAmortised O(1)O(1)
Insert or remove in the middleO(n) (shifts elements)O(1) once positioned, but finding the position is O(n)
Memory and cacheCompact, CPU-cache friendlyAbout 24+ bytes overhead per node, pointer chasing

HashMap internals#

HashMap put(key, value)
flowchart LR
    K["key.hashCode()"] --> SPREAD["spread: h ^ (h >>> 16)"] --> IDX["index = hash & (n - 1)"] --> B{"bucket empty?"}
    B -->|"yes"| NEW["store node"]
    B -->|"no"| EQ{"same key?<br/>(hash and equals)"}
    EQ -->|"yes"| REPL["replace value"]
    EQ -->|"no"| CHAIN["append to chain;<br/>treeify if chain ≥ 8 and table ≥ 64"]
    NEW --> SIZE{"size > capacity × 0.75?"}
    CHAIN --> SIZE
    SIZE -->|"yes"| RESIZE["double table, split buckets"]
  • Table: an array of buckets whose length is a power of two (default 16). The index is hash & (n-1).
  • Hash spreading: the high bits are XOR-ed into the low bits, so poor hashCodes spread better.
  • Collisions: stored as a linked list, which treeifies into a red-black tree when one bucket reaches 8 entries (and the table has at least 64 buckets). The worst case improves from O(n) to O(log n).
  • Load factor 0.75: when size exceeds capacity × 0.75 the table doubles, and each entry either stays or moves by exactly oldCapacity.
  • Pre-size when you know the count: HashMap.newHashMap(expectedSize) (Java 19+) computes the right capacity.
  • One null key is allowed. Not thread-safe. Use ConcurrentHashMap for concurrent access.

The equals and hashCode contract#

  • Equal objects must have equal hash codes.
  • Hash codes should be stable while the object is a key, so never mutate a key after insertion or it becomes unreachable.
  • Records generate correct equals and hashCode automatically, which makes them excellent keys.

Other maps#

MapOrderStructureNotes
HashMapNoneHash tableFastest general purpose
LinkedHashMapInsertion (or access) orderHash table + doubly linked listAccess order + removeEldestEntry = an LRU cache
TreeMapSorted by keyRed-black tree, O(log n)NavigableMap: floorKey, ceilingEntry, subMap
EnumMapEnum ordinalArrayVery fast for enum keys
IdentityHashMapNoneUses ==Special cases (graphs, serialisation)
WeakHashMapNoneWeak keysEntries vanish when keys are garbage collected
ConcurrentHashMapNoneCAS + per-bin lockingThread-safe; see concurrency
// LRU cache in a few lines
class Lru<K, V> extends LinkedHashMap<K, V> {
    private final int max;
    Lru(int max) { super(16, 0.75f, true); this.max = max; }        // true = access order
    @Override protected boolean removeEldestEntry(Map.Entry<K, V> e) { return size() > max; }
}

Sets, queues and deques#

  • HashSet is a HashMap with dummy values. LinkedHashSet keeps insertion order. TreeSet is sorted (it uses a TreeMap).
  • ArrayDeque is a circular array and the best stack and queue. Do not use Stack or Vector (legacy and synchronised).
  • PriorityQueue is a binary heap: O(log n) offer and poll, ordered by a comparator. It does not sort on iteration.

Immutable and unmodifiable collections#

MethodResult
List.of(...), Set.of, Map.of, Map.ofEntriesTruly immutable, null-hostile, compact
List.copyOf(coll)Immutable copy (no copy if the source is already immutable)
Collections.unmodifiableList(list)A read-only view: changes to the backing list show through
stream.toList() (Java 16)Unmodifiable list

Iteration and fail-fast behaviour#

Most collections keep a modCount. Structural modification during iteration (other than through the iterator) throws ConcurrentModificationException on a best-effort basis. Use iterator.remove(), removeIf(...) or concurrent collections.

Choosing a collection#

NeedChoose
Indexed listArrayList
Unique elements, fast lookupHashSet
Key → value lookupHashMap
Sorted keys or range queriesTreeMap / TreeSet
Predictable iteration orderLinkedHashMap / LinkedHashSet
Stack or FIFO queueArrayDeque
Priority orderPriorityQueue
Shared between threadsConcurrentHashMap, CopyOnWriteArrayList, BlockingQueue
Constant dataList.of, Map.of

Common mistakes#

Quick check#

How does HashMap find a bucket? It spreads the key’s hashCode (h ^ (h >>> 16)) and computes hash & (n - 1), where n is the power-of-two table length.

What happens when many keys collide in one bucket? The chain grows. At 8 entries (with a table of at least 64) it becomes a red-black tree, so lookups become O(log n).

When does HashMap resize? When the size exceeds capacity × load factor (0.75 by default). The table doubles and entries are redistributed.

How do you build an LRU cache with the JDK? Use LinkedHashMap with access order (true in the constructor) and override removeEldestEntry.

What did Java 21 add to collections? Sequenced collections: SequencedCollection, SequencedSet and SequencedMap, with first and last operations and reversed().

List.of vs Collections.unmodifiableList? List.of is truly immutable and rejects nulls. unmodifiableList is a read-only view of a list that can still change underneath.

Summary#

  • Know the interfaces, then the implementations’ costs.
  • HashMap: power-of-two buckets, spread hash, treeified collisions, 0.75 load factor, doubling resize.
  • ArrayList, HashMap and ArrayDeque are the right default far more often than the alternatives.
  • Prefer immutable collections, and never mutate keys.

References#