HashMap vs Hashtable vs ConcurrentHashMap
Fast recall
Quick revision
Core answer
Understanding of thread-safety trade-offs across Map implementations and the ability to choose the right one for a given concurrency context
Recommending Hashtable for thread safety without realizing it locks the entire map on every operation, making it far slower than ConcurrentHashMap under real concurrent load
Explain that ConcurrentHashMap uses bucket-level (segment) locking and CAS operations so readers never block, whereas Hashtable synchronizes every method on the same monitor. Mention that Hashtable does not allow null keys or values, and that Collections.synchronizedMap wraps a HashMap with a single mutex — still coarser than CHM.
Interview-ready guide
Interview answer
HashMap, Hashtable, and ConcurrentHashMap all implement the Map interface, but they differ fundamentally in their approach to thread safety — from no synchronization at all, to a single global lock on every method, to fine-grained per-bucket locking with lock-free reads.
HashMap:
- No synchronization — fastest for single-threaded use
- Corrupts silently under concurrent access (infinite loops, lost updates, wrong results)
Hashtable:
- Synchronizes every method with one lock — technically thread-safe, brutally slow
- One reader blocks all other threads; no valid use case in modern Java
ConcurrentHashMap (Java 8+):
- Empty bucket → CAS atomically places the first node (no lock)
- Non-empty bucket →
synchronizedon the first node of that bucket only; other buckets unaffected - Reads are completely lock-free — node values are volatile;
get()requires no lock - Null keys and values prohibited —
get()returning null must unambiguously mean absent; otherwisecontainsKey()thenget()is a broken pattern in concurrent code
Compound operations:
computeIfAbsent(),merge(),compute()execute atomically within the bucket lock — replacing the broken check-then-act pattern
I recommend ConcurrentHashMap for any shared mutable map and HashMap when the map is thread-confined. Hashtable has no valid use case in modern Java.
HashMap vs Hashtable vs ConcurrentHashMap
Direct Answer
HashMap is unsynchronized and fastest for single-threaded use. ConcurrentHashMap provides thread safety through bucket-level locking and CAS with no blocking for readers. Hashtable is a legacy class that synchronizes every method on a single lock — never use it in new code.
Common Mistakes & Pitfalls
Recommending Hashtable for thread safety without realizing it locks the entire map on every operation, making it far slower than ConcurrentHashMap under real concurrent load
Check your understanding
Answer these without looking back at the article.
- How does ConcurrentHashMap achieve thread safety without locking the entire map?
- Why doesn't ConcurrentHashMap allow null keys or values?
- What is the difference between Collections.synchronizedMap and ConcurrentHashMap?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.