ConcurrentHashMap
ConcurrentHashMap is a thread-safe hash map. Readers and writers can overlap. It does not allow null keys or values. Compound updates belong in compute, merge, or putIfAbsent, not in a home-grown get plus put.
Detailed Explanation
HashMap is not safe for concurrent use. Wrapping it with Collections.synchronizedMap locks the whole table on every call and still does not make if (!map.containsKey(k)) map.put(k, v) atomic. ConcurrentHashMap is built for many threads. It stripes its internal structure so most operations do not share a lock, and retrievals are typically non-blocking.
Visibility follows the concurrent-map contract: a get that happens after a completed put in happens-before order sees the value. Iteration is weakly consistent: it does not throw ConcurrentModificationException, and it reflects some but not necessarily all updates that happen during the walk.
null is banned. That lets get return null only for "absent". Atomic helpers are the API you should prefer: putIfAbsent, replace(key, old, new), compute, computeIfAbsent, merge. computeIfAbsent runs the mapping function at most once per key in a well-behaved way, which is the right place to build an expensive value. Do not call back into the same map from that function; you can deadlock or recurse.
Size is O(n) in some JDK versions (it may aggregate counters) and is an estimate under concurrency. keySet, values, and entrySet are live views. forEach, search, and reduce can run in parallel with a parallelism threshold.
You reach for ConcurrentHashMap when many threads share a map and you can express updates as single map operations. You keep a HashMap behind a lock when the critical section spans several structures. You do not use ConcurrentHashMap as a bounded cache; it grows without eviction (unless you add your own). You do not use it when you need insertion order.
The cost of treating get plus put as atomic is a lost update. The cost of a huge map with no bound is memory. The cost of computeIfAbsent that does I/O while holding internal locks for that bin is latency for other writers of the same key. Keep mapping functions short.
Code Examples
import java.util.concurrent.ConcurrentHashMap;
public class Hits {
public static void main(String[] args) {
ConcurrentHashMap<String, Integer> hits = new ConcurrentHashMap<>();
hits.merge("home", 1, Integer::sum);
hits.merge("home", 1, Integer::sum);
System.out.println(hits.get("home")); // prints: 2
System.out.println(hits.get("missing")); // prints: null
}
}import java.util.concurrent.ConcurrentHashMap;
public class Cache {
public static void main(String[] args) {
ConcurrentHashMap<String, String> cache = new ConcurrentHashMap<>();
String value = cache.computeIfAbsent("user:1", key -> "Ana");
System.out.println(value); // prints: Ana
System.out.println(cache.computeIfAbsent("user:1", key -> "other")); // prints: Ana
}
}Common Gotchas
`null` keys or values throw `NullPointerException`
unlike `HashMap`.
`if (!map.containsKey(k)) map.put(k, v)` races
use `putIfAbsent` or `computeIfAbsent`.
Iterators are weakly consistent
they never throw CME and may skip concurrent updates.