Command Palette

Search for a command to run...

Phase 9Intermediate10 of 17 in Core Java

The Collections Framework

Lists, sets, maps, queues and heaps from the inside: ArrayList growth, HashMap buckets and treeify, TreeMap, PriorityQueue, iterators and Big-O.

Almost every Java program keeps groups of things: the items in a cart, the users online, the word counts in a document, the jobs waiting to run. The Collections Framework in java.util is the standard toolbox for that: List for ordered sequences, Set for "no duplicates", Map for looking things up by key, and Queue/Deque for "next in line". Picking the right one, and knowing what it costs, is one of the most practical skills in Java and one of the most asked-about in interviews.

This phase opens every box. You'll see how ArrayList grows by 1.5x with System.arraycopy, why LinkedList is rarely the answer, how HashMap turns a key into a bucket (hashing, spreading, load factor 0.75, resizing, and turning long buckets into red-black trees since Java 8), how TreeMap keeps keys sorted, and how PriorityQueue is a binary heap living in an array. Then the rules that bite in real code: fail-fast iterators and ConcurrentModificationException, Comparable versus Comparator.comparing (Java 8), immutable collections with List.of (Java 9) and List.copyOf (Java 10), and the sequenced collections added in Java 21.

It builds on Phase 3 (arrays), Topic 4.8 (equals and hashCode, the contract every hash-based collection depends on) and Phase 8 (generics, because every collection is a generic type). Phase 10's streams consume and produce collections, Topic 13.8 covers the thread-safe ones, and the DSA course (/dsa/hashing, /dsa/heaps, /dsa/queue-deque) puts all of it to work on real problems.

0/13 · 0%
13 topics ~6 h 55 code blocks & diagrams
Start with the first topic
1
9.1

The Collections Map

The Collections Framework is a family of interfaces (List, Set, Queue, Deque, Map) and the classes that implement them (ArrayList, HashSet, HashMap, ArrayDeque and more). You choose an interface for the behaviour you need and an implementation for the performance you need.

30 min 4 code practice

2
9.2

ArrayList Inside Out

ArrayList is a growable array: an Object[] plus a size counter. Reading by index is O(1), adding at the end is amortised O(1) because the array grows by 1.5x when full, and inserting or removing in the middle is O(n) because System.arraycopy shifts every later element.

34 min 5 code practice

3
9.3

LinkedList

LinkedList is a doubly linked list: a chain of node objects, each pointing to the one before and after. Adding or removing at either end is O(1), but reaching index i means walking node by node, so get(i) is O(n), and in practice ArrayList or ArrayDeque is almost always the better choice.

29 min 4 code practice

4
9.4

HashMap Internals

A HashMap is an array of buckets. It turns each key's hashCode into a bucket index (after mixing the high bits into the low ones), keeps colliding entries in a linked list that becomes a red-black tree past 8 nodes (Java 8+), and doubles the array when it's 75% full, giving O(1) expected get and put.

39 min 5 code practice

5
9.5

HashSet, LinkedHashSet and TreeSet

A Set holds each element at most once. HashSet is a HashMap with only keys (O(1), no order), LinkedHashSet adds insertion order, and TreeSet keeps elements sorted in a red-black tree (O(log n)) with navigation methods like floor and ceiling.

29 min 4 code practice

6
9.6

LinkedHashMap and TreeMap

LinkedHashMap is a HashMap that also remembers order (insertion or, optionally, access order), which makes a five-line LRU cache. TreeMap keeps keys sorted in a red-black tree and answers range and nearest-key questions in O(log n).

31 min 4 code practice

7
9.7

Queue, Deque and ArrayDeque

A Queue hands elements out in arrival order (first in, first out); a Deque works at both ends, so it can also be a stack (last in, first out). ArrayDeque, a circular array, is the fastest general implementation of both and replaces Stack and, for queues, LinkedList.

29 min 4 code practice

8
9.8

PriorityQueue

PriorityQueue always hands out the smallest element first (or the largest, with a reversed comparator). It's a binary heap stored in an array: peek is O(1), offer and poll are O(log n), and its iteration order is not sorted.

32 min 5 code practice

9
9.9

Iterators and Fail-Fast Behaviour

An Iterator walks a collection one element at a time and is what every for-each loop uses behind the scenes. Most java.util iterators are fail-fast: if the collection is structurally changed by anything other than the iterator itself, the next step throws ConcurrentModificationException.

30 min 4 code practice

10
9.10

Comparable and Comparator

Comparable gives a class one built-in natural order (compareTo), and a Comparator is a separate object that describes any other order. Sorting, TreeMap, TreeSet, PriorityQueue, min and max all work through one of the two.

27 min 5 code practice

11
9.11

Immutable and Unmodifiable Collections

List.of, Set.of and Map.of (Java 9) create collections that can never change, and List.copyOf (Java 10) makes a frozen copy. Collections.unmodifiableList is different: a read-only view that still shows changes made to the original list underneath.

25 min 4 code practice

12
9.12

Sequenced Collections

Java 21 added SequencedCollection, SequencedSet and SequencedMap: one standard way to get the first and last element, add at either end, and walk in reverse with reversed(), for every collection that has a defined order.

17 min 3 code practice

13
9.13

Choosing the Right Collection

Choosing a collection comes down to four questions: duplicates or not, does order matter (and which order), how will you look things up, and do several threads share it? The answers point to ArrayList, HashMap, HashSet, LinkedHashMap, TreeMap, ArrayDeque or PriorityQueue almost every time.

20 min 4 code practice