HashMap vs Hashtable in Java
HashMap vs Hashtable in Java explained: synchronization, null keys, performance and why ConcurrentHashMap replaces Hashtable, with code and interview answers.
Expected Interview Answer
HashMap and Hashtable both store key-value pairs, but HashMap is unsynchronized, allows one null key and multiple null values, and is faster, while Hashtable is synchronized (thread-safe), rejects nulls, and is a legacy class.
HashMap was introduced in Java 1.2 as part of the Collections Framework and is the default choice for single-threaded or externally-synchronized code. Hashtable predates the framework and synchronizes every method, which serializes all access and hurts throughput. For thread-safe maps today you use ConcurrentHashMap, which locks only segments/buckets rather than the whole table, so Hashtable is effectively obsolete.
- HashMap is faster because it is not synchronized
- HashMap allows one null key and many null values
- Hashtable is thread-safe but locks the whole table
- ConcurrentHashMap is the modern thread-safe replacement for Hashtable
- HashMap integrates with the Collections Framework and iterators are fail-fast
AI Mentor Explanation
Think of a HashMap as informal backyard scoring where anyone can jot runs against a batter instantly with no umpire gatekeeping, so it is quick but chaos erupts if two scorers write at once. Hashtable is like an official match where a single umpire signals every run one at a time, guaranteeing no clash but slowing everything to that one umpire's pace, and blank unnamed batters are simply not allowed on the sheet.
Step-by-Step Explanation
Step 1
Synchronization
Hashtable synchronizes every method so it is thread-safe; HashMap does no synchronization and is faster in single-threaded use.
Step 2
Null handling
HashMap allows one null key and any number of null values; Hashtable throws NullPointerException on a null key or value.
Step 3
Performance
HashMap avoids lock overhead and generally outperforms Hashtable, whose whole-table locking serializes access.
Step 4
Legacy vs modern
Hashtable is a legacy class from Java 1.0; HashMap arrived with the Collections Framework in Java 1.2.
Step 5
Thread-safe alternative
For concurrent code prefer ConcurrentHashMap, which locks only buckets/segments instead of the entire map.
What Interviewer Expects
- Knowing HashMap is unsynchronized and Hashtable is synchronized
- Explaining null key and null value behavior for each
- Recommending ConcurrentHashMap over Hashtable for concurrency
- Awareness that Hashtable is a legacy class
- Understanding the performance trade-off of whole-table locking
Common Mistakes
- Claiming HashMap is thread-safe by default
- Saying Hashtable allows null keys or values
- Recommending Hashtable instead of ConcurrentHashMap for concurrency
- Believing the two have identical performance
- Confusing fail-fast HashMap iterators with Hashtable's enumerator
Best Answer (HR Friendly)
“Both store data as key-value pairs, but HashMap is faster and meant for single-threaded work, while Hashtable is an older, slower version that is safe for multiple threads. In modern code, developers use HashMap normally and ConcurrentHashMap when they need thread safety.”
Code Example
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
public class MapDemo {
public static void main(String[] args) {
Map<String, String> hashMap = new HashMap<>();
hashMap.put(null, "allowed"); // one null key is fine
hashMap.put("key", null); // null values are fine
System.out.println(hashMap);
Map<String, String> hashtable = new Hashtable<>();
try {
hashtable.put(null, "x"); // throws NullPointerException
} catch (NullPointerException e) {
System.out.println("Hashtable rejects null keys");
}
}
}import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;
Map<String, Integer> counts = new ConcurrentHashMap<>();
counts.merge("apples", 1, Integer::sum); // atomic, no whole-table lockFollow-up Questions
- How does ConcurrentHashMap achieve thread safety without locking the whole map?
- What is a fail-fast iterator and does Hashtable have one?
- How does HashMap resolve hash collisions in Java 8 and later?
- Why should equals() and hashCode() be consistent for map keys?
- When would you still choose a synchronized wrapper like Collections.synchronizedMap()?
MCQ Practice
1. Which statement about HashMap is correct?
HashMap permits exactly one null key and any number of null values, and it is not synchronized.
2. What happens when you put a null key into a Hashtable?
Hashtable does not accept null keys or values and throws a NullPointerException.
3. Which class is the recommended thread-safe map today?
ConcurrentHashMap locks only buckets/segments, giving thread safety with far better concurrency than Hashtable.
Flash Cards
Is HashMap synchronized? — No. HashMap is unsynchronized; use ConcurrentHashMap or external synchronization for threads.
Does Hashtable allow null keys? — No. Hashtable throws NullPointerException for null keys and null values.
When was HashMap introduced? — Java 1.2, as part of the Collections Framework; Hashtable predates it (Java 1.0).
Modern replacement for Hashtable? — ConcurrentHashMap, which locks buckets instead of the entire table.