Use HashMap for single-threaded code and ConcurrentHashMap when
threads share the map. Do not use Hashtable in new code. The differences that
actually matter: HashMap accepts a null key and null values, Hashtable
throws NullPointerException for either; Hashtable‘s methods are
synchronized and HashMap‘s are not; and Hashtable is a legacy
class kept for compatibility. Every claim below was run on OpenJDK 26.0.2 and the output pasted
unedited.
Table of Contents
Nulls
HashMap.put(null,v) then containsKey(null) -> true
HashMap.get(null) -> null-key-ok
HashMap.put(k,null) then get("k") -> null
HashMap size -> 2
Hashtable.put(null,v) -> java.lang.NullPointerException
Hashtable.put(k,null) -> java.lang.NullPointerException
ConcurrentHashMap.put(null,v) -> java.lang.NullPointerException
ConcurrentHashMap.put(k,null) -> java.lang.NullPointerException
HashMap permits exactly one null key and any number of null values.
Hashtable rejects both. Note the third line: for HashMap,
get() returning null is ambiguous — it means either “absent” or “present and
mapped to null”. Use containsKey() to tell them apart. ConcurrentHashMap
rejects nulls too, precisely so that get() returning null is unambiguous in concurrent
code.
Synchronization
java.util.HashMap put() synchronized -> false
java.util.Hashtable put() synchronized -> true
java.util.concurrent.ConcurrentHashMap put() synchronized -> false
Hashtable.get() synchronized -> true
HashMap.get() synchronized -> false
That is read off the method modifiers with reflection, not from documentation.
ConcurrentHashMap.put is not a synchronized method — it uses compare-and-swap
with per-bin locking, which is why it scales where Hashtable‘s single lock does not.
The part most articles get wrong: synchronized ≠ thread-safe
Hashtable makes each individual call atomic. It does nothing for a pair of calls.
Eight threads each incrementing a shared counter 20,000 times, so the correct total is 160,000:
-- Hashtable, get-then-put (both calls synchronized) --
run 1: 39584
run 2: 39992
run 3: 28452
-- Collections.synchronizedMap(HashMap), get-then-put --
run 1: 36885
run 2: 30051
run 3: 23455
-- ConcurrentHashMap.merge(), one atomic op --
run 1: 160000
run 2: 160000
run 3: 160000
Three-quarters or more of the increments vanish. These are race outcomes, so the exact numbers
differ every run — the pattern (large loss for get-then-put, exact for merge) is the
reproducible part, not the digits. Swapping Hashtable for
Collections.synchronizedMap(new HashMap<>()) changes nothing, because the bug is in
the calling code, not the map. merge, compute,
computeIfAbsent and putIfAbsent on ConcurrentHashMap fix it by
doing the whole read-modify-write under one lock.
Where Hashtable‘s locking does help is concurrent writes of distinct keys, which an
unsynchronized HashMap handles by silently losing data:
-- unsynchronized HashMap, concurrent puts of DISTINCT keys --
run 1: size = 95757 (expected 160000)
run 2: size = 64307 (expected 160000)
run 3: size = 32832 (expected 160000)
-- Hashtable, concurrent puts of DISTINCT keys --
run 1: size = 160000 (expected 160000)
run 2: size = 160000 (expected 160000)
run 3: size = 160000 (expected 160000)
Worse than the lost entries: this run also left worker threads spinning inside the corrupted
HashMap and the JVM would not exit on its own. The verification script for this page uses
daemon threads and an explicit System.exit for exactly that reason. A HashMap
written by several threads is not merely inaccurate, it can hang.
Iteration: fail-fast Iterator vs legacy Enumeration
Hashtable.keys() Enumeration after structural change -> completed, NO exception
Hashtable.keySet().iterator() -> java.util.ConcurrentModificationException
HashMap.keySet().iterator() -> java.util.ConcurrentModificationException
ConcurrentHashMap.keySet().iterator() -> completed, NO exception
This is the detail worth taking away. It is not “Hashtable uses Enumeration, HashMap uses Iterator”
— Hashtable has both, and they behave differently. The legacy
Enumeration from keys() is not fail-fast and will quietly keep going over a
map that changed underneath it. The Iterator from the collection views is fail-fast.
ConcurrentHashMap‘s iterator is weakly consistent by design: it never throws, and reflects
some but not necessarily all concurrent updates.
Capacity, load factor and growth
HashMap table length after first put -> 16
HashMap loadFactor -> 0.75
Hashtable table length at construction -> 11
Hashtable loadFactor -> 0.75
Hashtable capacities: 11 -> 23 -> 47 -> 95
HashMap capacities: 16 -> 32 -> 64
Both default to a 0.75 load factor. Hashtable starts at 11 and grows to
2n+1; HashMap starts at 16 and doubles, keeping the table a power of two so
it can index buckets with a bitmask instead of a modulo. HashMap also allocates its table
lazily — on the first put, not in the constructor.
Reading those private fields required --add-opens java.base/java.util=ALL-UNNAMED.
Without it, OpenJDK 26 refuses:
java.lang.reflect.InaccessibleObjectException: Unable to make field int java.util.HashMap.threshold
accessible: module java.base does not "opens java.util" to unnamed module
Ordering
insertion order : [one, two, three, four, five, six, seven, eight]
HashMap order : [six, four, one, seven, two, three, five, eight]
Hashtable order : [five, eight, six, two, one, seven, three, four]
LinkedHashMap : [one, two, three, four, five, six, seven, eight]
TreeMap : [eight, five, four, one, seven, six, three, two]
Neither preserves insertion order, and neither ordering is stable across versions or key sets — do
not rely on either. Use LinkedHashMap for insertion order or TreeMap for
sorted order.
Legacy, not deprecated
Hashtable has @Deprecated -> false
Hashtable superclass -> java.util.Dictionary
HashMap superclass -> java.util.AbstractMap
Dictionary has @Deprecated-> false
A correction worth stating plainly, because this page previously implied otherwise and most
articles on the topic do too: Hashtable is not annotated
@Deprecated and neither is Dictionary, so using them produces no compiler
warning. They are “legacy” — retained for compatibility, superseded in practice. The reason to avoid
Hashtable is that its single lock is slower than ConcurrentHashMap under
contention while still not making your compound operations correct, not that the compiler objects.
Summary
| HashMap | Hashtable | ConcurrentHashMap | |
|---|---|---|---|
| Null key / null value | One null key, many null values | NPE for both | NPE for both |
| Methods synchronized | No | Yes (put, get) |
No — CAS + per-bin locks |
| Default capacity / load factor | 16 (lazy) / 0.75 | 11 (eager) / 0.75 | 16 / 0.75 |
| Growth | Doubles | 2n+1 | Doubles |
| Iterator on structural change | Fail-fast (CME) | Fail-fast via keySet(); keys() Enumeration is not |
Weakly consistent, never throws |
| Superclass | AbstractMap |
Dictionary |
AbstractMap |
| Use it when | Single thread, or externally confined | Never in new code | Threads share the map |
Verified 2026-08-09 on OpenJDK 26.0.2 (Homebrew build, aarch64, macOS 26.6). Every block above is unedited program output. This script compiles and runs both programs and checks each claim. This page runs longer than usual because the output is pasted rather than summarised. Not verified here: any performance comparison between the three maps — the concurrency figures above demonstrate correctness, not throughput, and a throughput claim would need JMH.