Java / Java Concurrency and Multi Threading in Java 17 and Java 21 Interview questions
What is the difference between ConcurrentHashMap and Hashtable?
| ConcurrentHashMap | Hashtable |
| Locks per-bin; high concurrency for reads and writes. | Synchronizes every method on a single lock for the whole map. |
| Iterators are weakly consistent and never throw ConcurrentModificationException. | The single shared lock itself becomes the concurrency bottleneck. |
| Disallows null keys and null values. | Also disallows null keys and null values. |
| Modern class, actively used and improved since Java 5/8. | Legacy class from Java 1.0, effectively superseded. |
The practical difference is performance under concurrent access: because Hashtable serializes every get and put through one lock, it becomes a bottleneck as thread count grows, while ConcurrentHashMap's fine-grained locking lets unrelated operations proceed in parallel. For new code, ConcurrentHashMap is essentially always the right choice.
More Related questions...