What is ConcurrentHashMap and how does it differ from HashMap?
Why Interviewers Ask This
Advanced questions like this reveal whether a candidate has internalized Java deeply enough to make architectural decisions. Strong answers demonstrate both breadth and depth of experience.
Answer
ConcurrentHashMap is a thread-safe implementation of HashMap designed for high-concurrency scenarios. Unlike Hashtable (which locks the entire map), ConcurrentHashMap uses lock striping — in Java 8, it uses CAS (Compare-And-Swap) operations and synchronized blocks on individual buckets, allowing concurrent reads and writes to different buckets simultaneously. This gives far better throughput than full synchronization. Reads (get) never block. ConcurrentHashMap does not allow null keys or values (to avoid ambiguity between a missing key and a key mapped to null). For atomic compound operations, use methods like putIfAbsent(), computeIfAbsent(), and merge().
Pro Tip
This topic has Java-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.