Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

Compare Hashtable, Collections.synchronizedMap and ConcurrentHashMap for a shared map in a service under high concurrency. Is there any case where the synchronized wrapper is the better choice?

Hashtable and Collections.synchronizedMap both put one lock around every operation, so under high concurrency they serialize all threads, readers included. ConcurrentHashMap lets reads proceed without locking and lets writes to different bins run in parallel, so it is the right default for a shared map in a busy service. The synchronized wrapper is still the better choice when we need to hold one lock across several operations to get an atomic multi-step view, which ConcurrentHashMap cannot give, or when we must store null keys or values, which ConcurrentHashMap forbids. Hashtable is legacy and should not be used in new code.

Why this question is asked

Every Java codebase of any age contains all three of these maps, and engineers often treat them as interchangeable ways of saying thread-safe. Under real load they behave very differently: one of them scales with cores, the other two line every thread up behind one lock. Interviewers ask this comparison because it shows whether we understand where contention comes from, and the second half of the question checks whether we can name the rare cases where the slower option is actually correct. In this answer, we will recap the locking of each map, compare them under concurrency, look at the multi-step and null cases where the wrapper wins, and explain why Hashtable has no place in new code.

1A Quick Recap of the Three Maps

Hashtable dates from Java 1.0. Every public method is declared synchronized, so every call locks the Hashtable object itself. It predates the collections framework and was later retrofitted to implement Map, which is why it also has old methods like elements() and keys() returning Enumeration.

Collections.synchronizedMap, from Java 1.2, wraps any Map, typically a HashMap, in an object whose every method locks the wrapper and then delegates. Locking-wise it behaves like Hashtable, but it is a decorator over the modern collections, so it works with any map implementation, including a LinkedHashMap or a TreeMap.

ConcurrentHashMap, from Java 5 and rewritten in Java 8, is designed for concurrency. get never locks; it reads volatile fields. Writes to an empty bin use compare-and-set, and writes to an occupied bin lock only that bin's first node. Counting is spread across cells to avoid a single hot counter.

2What Happens Under High Concurrency

Picture a service with 64 request threads sharing a map of 100,000 product entries, with 95 percent reads. With Hashtable or the wrapper, every get acquires the same monitor. Only one thread can be inside the map at any moment, so the map runs at single-thread speed, and the lock is constantly contended, which makes each acquisition far more expensive than the lookup itself. Thread dumps show many threads BLOCKED on the map object.

With ConcurrentHashMap, the 95 percent of reads never take a lock and run truly in parallel on all cores. The 5 percent of writes lock only their own bin, and with thousands of bins two writers rarely collide. Throughput grows with the number of cores instead of staying flat.

Java
Map<String, Product> legacy = new Hashtable<>(); // one lock for everything
Map<String, Product> wrapped = Collections.synchronizedMap(new HashMap<>()); // one lock for everything
Map<String, Product> modern = new ConcurrentHashMap<>(); // lock-free reads, per-bin writes
// 64 threads, 95% get / 5% put, rough shape of a JMH result
// legacy: ~flat as threads increase, many threads BLOCKED
// wrapped: ~flat as threads increase, many threads BLOCKED
// modern: scales close to the number of cores
Three thread-safe maps
PropertyHashtablesynchronizedMapConcurrentHashMap
LockingOne lock, every methodOne lock, every methodLock-free reads, lock per bin
Concurrent readsNoNoYes
Atomic compound methodsJava 8 defaults, lockedJava 8 defaults, lockedYes, per key
Lock across several callssynchronized on itsynchronized on wrapperNot possible
IterationLock manually or CMELock manually or CMEWeakly consistent, no lock
Null keys or valuesNot allowedAllowed if inner map allowsNot allowed

3When the Synchronized Wrapper Is the Better Choice

The wrapper's single lock is a weakness for throughput but a strength for atomicity across calls. Because its lock is the wrapper object, we can hold it ourselves across several operations, and during that block no other thread can read or change the map. That gives an atomic multi-step view, which ConcurrentHashMap cannot provide, because it has no lock covering the whole map.

Java
Map<String, Long> balances = Collections.synchronizedMap(new HashMap<>());
void transfer(String from, String to, long amount) {
synchronized (balances) { // no reader sees money in flight
long a = balances.get(from);
if (a < amount) throw new IllegalStateException("insufficient funds");
balances.put(from, a - amount);
balances.merge(to, amount, Long::sum);
}
}
long total() {
synchronized (balances) { // a consistent total across all accounts
return balances.values().stream().mapToLong(Long::longValue).sum();
}
}

With ConcurrentHashMap, each of those calls would be atomic on its own, but a concurrent total() could see the money removed from one account and not yet added to the other. When an invariant spans several keys and contention is modest, the wrapper is a simple and correct tool. At high contention, the better design is usually an immutable snapshot swapped atomically, or moving the invariant into a database transaction.

The second case is nulls. If the map must hold null values, perhaps to cache the fact that a lookup found nothing, or null keys from legacy code, a synchronized HashMap allows them and ConcurrentHashMap throws NullPointerException. Usually a sentinel value or Optional is a cleaner fix, but the wrapper is the direct option.

4Why Hashtable Is Legacy

Hashtable offers nothing the wrapper does not, and it carries baggage: Enumeration methods, a confusing contains method that checks values, not keys, and synchronized methods that cost a little even in single-threaded code. It also rejects nulls, so it has neither the wrapper's flexibility nor ConcurrentHashMap's scalability. In new code, use HashMap when the map is not shared, ConcurrentHashMap when it is, and the synchronized wrapper only for the multi-step cases above. Hashtable survives in old APIs such as java.util.Properties, which extends it, and that is the only reason to meet it today.

5Follow-up questions

Why does ConcurrentHashMap forbid null?

Because in a concurrent map, get returning null must mean absent unambiguously. With nulls allowed, a caller could not tell absent from mapped to null without a second call to containsKey, and between the two calls another thread could change the map, so the answer would be unreliable.

Is the Java 8 ConcurrentHashMap still using segments?

No. Java 7 and earlier split the map into 16 segments by default, each a small hash table with its own lock. Java 8 removed segments in favour of locking individual bins and compare-and-set on empty ones, which gives much finer granularity. The concurrencyLevel constructor argument survives only as a sizing hint.

Is synchronizedMap faster than ConcurrentHashMap with a single thread?

Very slightly in some microbenchmarks, because an uncontended lock is cheap and HashMap is simple. The difference is small enough that it rarely justifies the choice; pick by sharing pattern, not by single-thread speed.

6Conclusion

In this answer, we compared the three maps. Hashtable and Collections.synchronizedMap both use one lock for every operation, so under high concurrency they serialize all threads, while ConcurrentHashMap allows lock-free reads and parallel writes to different bins. The synchronized wrapper is still the better choice when we need to hold a lock across several operations for an atomic multi-step view, or when we need null keys or values. Hashtable is legacy, with no advantage over the alternatives, and should not be used in new code.

What to say in the interviewAbout 90 seconds aloud

Open with

Hashtable and Collections.synchronizedMap behave the same way under load: every method takes one lock on the whole map, so all threads, even pure readers, take turns. On a busy service with dozens of threads, the map runs at the speed of one thread and you see many threads BLOCKED on it in thread dumps.

Then, if they want more

ConcurrentHashMap is built differently. get doesn't lock at all, it reads volatile fields, and writes only lock the bin the key falls into, or use compare-and-set when the bin is empty, so reads scale across cores and writes to different keys rarely contend. It also has atomic putIfAbsent, compute and merge, and iteration needs no external lock.

So for a shared map in a high-concurrency service, ConcurrentHashMap is the default. There are two cases where the synchronized wrapper is better. First, when I need an atomic view across several operations.

Because the wrapper's lock is the wrapper object, I can synchronize on it and do a transfer between two keys, or compute a consistent total, and nobody sees the intermediate state. ConcurrentHashMap can't do that because there's no whole-map lock. Second, nulls: the wrapper around a HashMap allows null keys and values, and ConcurrentHashMap doesn't.

Hashtable has no advantage over either; it's a Java 1.0 legacy class with old Enumeration methods, and I wouldn't use it in new code. It only appears today through things like Properties.

About this answer

Last updated
12 November 2026
Versions it names
Java 8, Java 5, Java 7
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Compare Hashtable, Collections.synchronizedMap and ConcurrentHashMap for a shared map in...": https://scrivio-interview-library.netlify.app/java/concurrent-collections/0861af4ff3/