← Back to Java

HashMap vs Hashtable

JavaMiddata-structuresjava

The Question

What is the difference between HashMap and Hashtable?

What a Strong Answer Covers

  • HashMap not synchronized allows null
  • Hashtable synchronized no null
  • Hashtable legacy Java 1.0
  • HashMap faster
  • prefer ConcurrentHashMap for thread safety

Senior-Level Answer

HashMap and Hashtable both implement the Map interface and store key-value pairs using a hash table, but they differ in synchronization, null handling, and historical context.

Hashtable is a legacy class from Java 1.0. Every public method is synchronized, meaning only one thread can access the table at a time. This makes Hashtable thread-safe but slow — every operation acquires a monitor lock on the entire object, even reads. Hashtable does not allow null keys or null values.

HashMap was introduced in Java 1.2 as part of the Collections Framework. It is not synchronized, making it faster in single-threaded scenarios. HashMap allows one null key and any number of null values.

The critical follow-up: what should you use for thread-safe map operations? ConcurrentHashMap, not Hashtable. ConcurrentHashMap uses fine-grained locking (node-level CAS operations in Java 8+), allowing multiple threads to read and write concurrently without blocking each other. It also provides atomic compound operations like putIfAbsent() and computeIfAbsent().

Another option is Collections.synchronizedMap(), which wraps a HashMap with synchronized methods — similar to Hashtable's coarse-grained locking but returns a standard Map interface.

HashMap's iterators are fail-fast (throw ConcurrentModificationException on structural modification during iteration). ConcurrentHashMap's iterators are weakly consistent — they reflect some changes and never throw ConcurrentModificationException.

There is essentially no reason to use Hashtable in modern Java code.

Key Differences

AspectHashMapHashtable
SynchronizationNot synchronizedAll methods synchronized
Null keys/valuesOne null key, many null valuesNo nulls allowed
PerformanceFaster (no locking overhead)Slower (coarse-grained locking)
Introduced inJava 1.2 (Collections Framework)Java 1.0 (legacy)
Parent classAbstractMapDictionary (legacy)
Iterator behaviorFail-fastEnumerator not fail-fast; Iterator fail-fast
Modern replacementUse directly for single-threadedUse ConcurrentHashMap instead

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Identifies synchronization and null-handling differences correctly. Mentions that Hashtable is legacy.

3/3 — Strong Answer

Covers synchronization, null handling, legacy status. Explains why ConcurrentHashMap replaces Hashtable. Mentions Collections.synchronizedMap as an alternative. Discusses iteration behavior.

Common Mistakes

  • Recommending Hashtable for thread safety instead of ConcurrentHashMap
  • Forgetting that HashMap allows null keys and values while Hashtable does not
  • Not explaining WHY Hashtable's synchronization is problematic — it's the granularity
  • Confusing Collections.synchronizedMap() with ConcurrentHashMap
  • Failing to mention ConcurrentHashMap at all

Follow-Up Questions

  • How does ConcurrentHashMap achieve better concurrency than Hashtable? — Node-level CAS operations and synchronized blocks on individual bins.
  • Can you use a HashMap safely in a multi-threaded environment? — Yes, if properly externally synchronized or effectively immutable after construction.
  • What is the difference between Collections.synchronizedMap() and ConcurrentHashMap? — synchronizedMap wraps with a single mutex. ConcurrentHashMap uses fine-grained locking and provides atomic compound operations.
  • Why does ConcurrentHashMap not allow null keys or values? — Ambiguity — null return from get() indistinguishable from missing key in concurrent context.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • public static void main

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout