Kayavak / The Backend / BE·35 Collections / Deeper
Deeper · The Backend
Collections
The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.
Why does this exist?
Java 1.0 (1996) had only Vector, Hashtable and arrays: every method synchronised whether you needed it or not, and
no common interfaces. Java 1.2 (1998) added the Collections Framework, designed by Joshua Bloch: interfaces (List,
Set, Map) separated from implementations (ArrayList, HashSet, HashMap), so code could depend on the shape of
the data and swap the structure. Java 5 (2004) made collections generic and added java.util.concurrent's concurrent
maps and queues; Java 8 (2014) added streams and made HashMap resist pathological hashing by turning long chains into
trees; Java 21 (2023) added sequenced collections with a defined first and last element.
The mental model
Try it yourself first: write your own model in the Mental Model Explorer, then compare.
Pick the interface by the operations you need, and the implementation by their cost on your data. The interface is the promise; the implementation is a data structure with trade-offs (Foundations, ch03).
| Question | Answer |
|---|---|
| What is it? | Interfaces for groups of objects (List, Set, Map, Queue, Deque) and their implementations |
| Problem it solves | Storing, finding and iterating over many objects without writing data structures each time |
| Parts | Interfaces, implementations (array-based, hash-based, tree-based, linked), iterators, views, immutable copies, concurrent variants |
| How it works | Hash structures rely on hashCode and equals; sorted ones on compareTo or a Comparator |
| Assumptions | Keys' hashCode, equals and ordering are consistent and don't change while stored |
| When they change | Mutating a key after inserting it makes the entry unfindable; inconsistent equals and compareTo give different answers from HashSet and TreeSet |
| Where it fails | Shared mutable collections across threads; boxed primitives' memory cost at scale |
| Related ideas | Data structures (Foundations, ch03); generics (ch36); memory (ch41) |
What if?
What if you used a mutable object as a HashMap key and then changed it?
Reason it through, then open
The entry stays in the bucket chosen by its old hash. Looking it up with the changed key computes a new hash and
searches a different bucket, so get returns null and containsKey says false, while iterating the map still shows
the entry. Keys should be immutable (strings, records of immutable fields) or never changed while stored.
What if equals and compareTo disagree?
Reason it through, then open
A HashSet uses equals, a TreeSet uses compareTo, so the same elements give different sets: BigDecimal's
1.0 and 1.00 are unequal by equals but compare as equal, so a HashSet keeps both and a TreeSet keeps one. The
Comparable documentation asks for consistency with equals for exactly this reason.
Where the boundary is
| Aspect | Unmodifiable view (Collections.unmodifiableList) |
Immutable copy (List.copyOf) |
|---|---|---|
| Can callers change it? | No | No |
| Can the original change underneath? | Yes: the view shows it | No: it's a snapshot |
| Use when | You want read-only access to live data | You want a value nobody can change |
How sure are we?
| Claim | How it's known | Where from |
|---|---|---|
| The Collections Framework arrived in Java 1.2 (1998) | Historical | JDK release notes |
| HashMap turns long bins into trees since Java 8 | Convention | OpenJDK source: chains above 8 entries in tables of 64+ buckets |
| ArrayList usually beats LinkedList | Measured | The chapter's lab; caches favour contiguous arrays |
Still open
Persistent (immutable, structurally shared) collections are standard in Clojure and Scala and absent from the JDK; whether Java should include them is a recurring proposal.
What would change your mind? If Valhalla let List<int> store numbers flatly, the memory argument for primitive
arrays over collections would mostly disappear.
Recall
What happens if you mutate a HashMap key after inserting it?
The entry stays under its old hash, so lookups with the changed key miss it.
Why must equals and compareTo be consistent?
Hash-based and sorted collections use different methods, so inconsistency gives them different contents.
What's the difference between an unmodifiable view and an immutable copy?
A view reflects changes to the original; a copy is a snapshot that never changes.
Why were the Java 1.0 collections replaced?
They synchronised every call and had no common interfaces.
Reflect
Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.