HashMap vs Hashtable vs ConcurrentHashMap
Fast recall
Quick revision
Core answer
HashMap is not thread-safe and permits one null key and null values. Hashtable is a legacy synchronized map and permits neither. ConcurrentHashMap is the usual concurrent choice: reads generally do not lock, and updates use CAS plus locking at a bucket/bin level when needed. Modern ConcurrentHashMap does not use the fixed segment design of early versions, and compound operations should use methods such as compute, merge, or putIfAbsent.Whether you know the thread-safety and performance trade-offs and the modern recommendation
Recommending Hashtable for thread safety, or thinking Collections.synchronizedMap equals ConcurrentHashMap
Explain ConcurrentHashMap's reads generally avoid locking and bins/bin-level write locking vs Hashtable's whole-map lock
Interview-ready guide
Interview answer
HashMap is the normal non-concurrent map. It allows one null key and null values, but concurrent writes without external coordination are unsafe. Hashtable synchronizes individual methods and rejects nulls, but it is a legacy design with one coarse API style; a sequence such as containsKey followed by put is still not atomic.
ConcurrentHashMap is the modern concurrent choice. Reads generally avoid locking, while updates use compare-and-set and coordination around the affected bin. The old explanation that it divides the map into fixed segments is outdated for current Java. It also provides compound methods such as putIfAbsent, compute, and merge, which are important because thread-safe individual methods do not make a multi-call workflow atomic.
HashMap vs Hashtable vs ConcurrentHashMap
Direct Answer
HashMap is not thread-safe and permits one null key and null values. Hashtable is a legacy synchronized map and permits neither. ConcurrentHashMap is the usual concurrent choice: reads generally do not lock, and updates use CAS plus locking at a bucket/bin level when needed. Modern ConcurrentHashMap does not use the fixed segment design of early versions, and compound operations should use methods such as compute, merge, or putIfAbsent.
Common Mistakes & Pitfalls
- Recommending
Hashtablefor thread safety. It works, but the whole-map lock makes it a bottleneck — useConcurrentHashMap. - Treating
Collections.synchronizedMapas equal. It wraps a single lock, so it serializes access just like Hashtable. - Putting nulls in the concurrent maps. Both
HashtableandConcurrentHashMapreject null keys and values. - Sharing a plain
HashMap. Concurrent writes can corrupt it or, in old JDKs, even spin forever.
Check your understanding
Answer these without looking back at the article.
- What is a fail-fast iterator?
- When would you use a LinkedHashMap instead?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.