3-minute skim before an interview. Full detail → HashMap Internals.
The 10 facts
- Structure: array of buckets; each bucket = one array slot holding a singly-linked list (or red-black tree at ≥8 entries, table ≥64).
- Default capacity 16, always a power of 2. Load factor 0.75 → resize (double) at 75% full.
- put:
hashCode()→ spreadh ^ (h>>>16)→ index(n-1) & hash→ walk bucket → replace or append. - get: same hash/index → jump to bucket → walk chain, match
hash==thenequals. - hashCode finds the bucket; equals finds the entry. (A bucket holds many colliding keys.)
- Contract: override
equals⇒ must overridehashCode. Else equal keys → different buckets →getnever finds them. - Same index, not same hash: collided keys usually have different hashCodes sharing low bits.
==on objects = reference identity;equals= logical. Not “primitives only.”- Why 31: odd (no bit loss), prime (spreads), cheap (
31*i == (i<<5)-i). - Resize (JDK 8): entry stays at
jor moves toj + oldCap, decided byhash & oldCap— no rehash.
Map family (one line each)
- HashMap — no order, O(1) avg.
- LinkedHashMap — insertion order (doubly-linked list).
- TreeMap — sorted keys, O(log n) (red-black tree).
- Map is not a Collection (stores pairs, not elements).
Drill cards
put steps in order?::hashCode → spread (h ^ h>>>16) → index (n-1)&hash → walk bucket → replace if key matches else append → treeify at 8 / resize at 0.75.
Why equals AND hashCode?::hashCode picks the bucket; equals finds the exact key in it. Break the contract → equal keys land in different buckets → get fails.
Why 31?::Odd (no bit loss vs even 32), prime (good spread), cheap (31*i = (i<<5)-i).
JDK 8 resize trick?::Capacity doubles; each entry stays at index j or moves to j+oldCap, chosen by one bit (hash & oldCap). No rehashing.
vs equals?:: = reference identity (same object); equals = logical equality. == works on objects too, comparing references.
HashMap vs LinkedHashMap vs TreeMap?::HashMap = no order; LinkedHashMap = insertion order (doubly-linked); TreeMap = sorted (red-black tree).
Treeify threshold?::A bucket’s chain converts to a red-black tree at 8 nodes when table ≥ 64 (else it resizes); reverts to list at 6.