How HashMap Works Internally
How HashMap works internally is probably the most asked Java question. You should explain hashing to buckets, collisions, treeification in Java 8, the load factor and resizing, and why HashMap is not thread-safe.
What you will learn
- Explain put and get step by step
- Explain collisions, linked lists and red-black trees
- Explain capacity, load factor and resizing
- Know null key handling
Key concepts
Buckets
An array of nodes (default capacity 16, always a power of two). The index is (n - 1) & hash, where hash spreads the high bits: h ^ (h >>> 16).
Collision
Different keys in the same bucket are kept in a linked list. Since Java 8 a bucket with more than 8 nodes (and table size at least 64) becomes a red-black tree, so the worst case is O(log n) instead of O(n).
Load factor
Default 0.75. When size exceeds capacity × load factor (12 for 16), the table doubles and entries are redistributed; in Java 8 each node either stays or moves by exactly the old capacity.
Null key
HashMap allows one null key (always bucket 0) and many null values. Hashtable and ConcurrentHashMap allow neither.
Step by step
put(key, value)
- Compute hash(key) and the bucket index
- Empty bucket → place a new node
- Otherwise walk the bucket: if a node has the same hash and equals(key), replace its value
- Else append a node (or insert into the tree); treeify if the bin is too long
- Increase size; resize if above the threshold
get(key)
Compute the same index, then compare hash first and equals second along the bucket. That is why both hashCode and equals must be correct.
Examples
A bad hashCode turns HashMap into a list
class BadKey {
final String v;
BadKey(String v) { this.v = v; }
@Override public int hashCode() { return 42; } // every key collides
@Override public boolean equals(Object o) { return o instanceof BadKey b && b.v.equals(v); }
}
// every put lands in one bucket → a tree at best, O(log n) instead of O(1)
// Pre-size when you know the count, to avoid resizes
Map<String, User> byEmail = HashMap.newHashMap(10_000); // Java 19+Practise it
Try these without looking back. If you can explain each one out loud, you are ready for this question.
- Explain step by step what happens when the 13th entry is put into a new HashMap.
- Explain why the capacity is always a power of two.
Interview questions
What interviewers ask next on this topic. Answer out loud first, then open the model answer.
Why is HashMap not thread-safe?
Concurrent puts can overwrite each other's nodes or both trigger a resize, losing entries; before Java 8 a concurrent resize could even create a cycle and an infinite loop. Use ConcurrentHashMap for shared maps.
HashMap vs Hashtable?
Hashtable is a legacy class with every method synchronized and no null keys or values. HashMap is unsynchronized and allows a null key. For concurrency use ConcurrentHashMap, not Hashtable.
What is the time complexity of HashMap get?
O(1) on average with a good hash. The worst case is O(log n) since Java 8 because long bins become red-black trees (it was O(n) before).
Summary
HashMap hashes keys into power-of-two buckets, resolves collisions with lists that become trees past 8 entries, resizes at 75% load, and relies on correct hashCode and equals. It is not thread-safe.
Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus