Difference Between HashMap and Hashtable
HashMap and Hashtable both store key-value pairs in a hash table and both implement the Map interface. The main differences are that Hashtable is synchronized and rejects null, while HashMap is not synchronized and accepts null. In this lesson, you will learn all the differences, see them in code, and learn which map to use for shared data.
Example: The null Difference
Let's see an example that stores null in both maps.
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
public class NullTest {
static void test(Map<String, String> map) {
String name = map.getClass().getSimpleName();
try {
map.put(null, "no key");
map.put("Navika", null);
System.out.println(name + ": accepted, " + map);
} catch (NullPointerException e) {
System.out.println(name + ": NullPointerException");
}
}
public static void main(String[] args) {
test(new HashMap<>());
test(new Hashtable<>());
}
}HashMap: accepted, {null=no key, Navika=null}
Hashtable: NullPointerExceptionExplanation: The same method is called with both maps. HashMap stores a null key and a null value. Hashtable throws as soon as it meets the null key.
Differences Between HashMap and Hashtable
| HashMap | Hashtable | |
|---|---|---|
| Synchronization | Not synchronized | Synchronized |
| Thread safety | Not thread-safe | Thread-safe for single calls |
| Performance | Faster | Slower, because of locking |
null keys | One allowed | Not allowed |
null values | Allowed | Not allowed |
| Introduced in | Java 1.2 | Java 1.0 (legacy) |
| Parent class | AbstractMap | Dictionary |
| Iteration | Fail-fast Iterator | Iterator and old Enumeration |
| Default capacity | 16, doubles | 11, grows to 2 x old + 1 |
| Long collision chains | Turned into trees (Java 8) | Stay as lists |
What to Use for Shared Maps
Hashtable locks the whole map for every call. ConcurrentHashMap was designed for threads: reads don't block, and writes lock only a small part of the map. It also provides atomic compound operations such as merge and computeIfAbsent.
| Situation | Use |
|---|---|
| One thread | HashMap |
| Many threads | ConcurrentHashMap |
| Old code that already uses it | Hashtable (only when you can't change it) |
Common Mistakes
Assuming Hashtable makes "check, then put" safe. Two separate calls can still interleave; use putIfAbsent or merge.
Moving code from HashMap to ConcurrentHashMap without checking for null. ConcurrentHashMap rejects null too.
Interview Corner
What is the difference between HashMap and Hashtable?
HashMap isn't synchronized, so it's faster but not thread-safe; it allows one null key and any number of null values. Hashtable is a legacy class from Java 1.0 whose methods are all synchronized; it allows no null keys or values and supports the old Enumeration. HashMap also converts long collision chains into trees. For concurrent use, ConcurrentHashMap is preferred over both.
Practice
- Easy: Replace the
Hashtablein the example with aConcurrentHashMapand predict the output before running it. - Harder: Use two threads to add 10,000 keys each to a shared
HashMapand to a sharedConcurrentHashMap. Compare the final sizes.
Key Takeaways
Was this tutorial helpful?