Why this matters
- Maps are the most used data structure in application code, and the convenience methods added in Java 8 remove most of the boilerplate people still write by hand.
- The check-then-act pattern on a map is both verbose and unsafe under concurrency, and there is a single-call replacement for every instance of it.
- Load factor and initial capacity are the two tuning knobs that matter, and they are easy to get right once.
table — array of buckets
bucket 0empty
bucket 1"asha" → 30
bucket 5 — collision chain
"ravi" → 24next →
"neha" → 29next → null
bucket 9 — treeified
Red-black treeMore than 8 entries: lookup becomes O(log n)
Bucket index comes from the key's hash, not its insertion order — which is why HashMap iteration has no predictable sequence.
The core operations
Loading compiler…
The three views
A map exposes its contents as three views — live windows, not copies.
Loading compiler…
Because they are views, removing from keySet() removes from the map. Adding is not supported, since a
key with no value is meaningless.
Iterating by entry is both clearer and faster than looking each value up again:
Loading compiler…
The rest of this guide is part of Pro
Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.
Still to come · 5 sections
- The methods that replace check-then-act
- Capacity and load factor
- Choosing a map
- Keys must be stable
- Common misreadings
Test yourself
Answer these before moving on — recall is what makes it stick.