HashMap and Hashtable both store key-value pairs using hashing, but HashMap is not synchronised, allows one null key and null values, and is faster. Hashtable is synchronised (thread-safe), allows no null key or value, and is a legacy class. In short, use HashMap by default, and for thread safety prefer ConcurrentHashMap over the old Hashtable.
HashMap vs Hashtable is a classic Java comparison, since both store key-value pairs through hashing. Both appear in every Java and interview syllabus. Developers often blur which one is thread-safe and which one accepts a null key.
The core split is synchronisation and null handling. Is the class locked for threads, and can a key be null? HashMap says no to both, while Hashtable says yes then no. This guide defines each class, shows how hashing works, compares them in detail, and explains when to use which.
If you are still mapping out Java collections, it also helps to know the difference between HashMap and HashSet.

What is a HashMap?
A HashMap is a Java class that stores data as key-value pairs using hashing. A hash function maps each key to a bucket in an internal array, so lookups run in roughly constant time. It belongs to the Java Collections Framework, and it is the everyday map for general use.
Crucially, HashMap is not synchronised, so it is fast in a single thread but unsafe if threads share it. It also allows one null key and any number of null values. When two keys hash to the same bucket, HashMap handles the collision with chaining, and it resizes automatically as it fills.
Advantages of HashMap:
- Fast retrieval, since there is no synchronisation overhead.
- Allows one null key and null values, which adds flexibility.
- Dynamic sizing, so it adapts to a varying amount of data.
- Part of the modern Collections Framework, with a fail-fast iterator.
Disadvantages of HashMap:
- Not thread-safe, so shared access needs external synchronisation.
- Frequent resizing can add some memory and time cost.
What is a Hashtable?
A Hashtable also stores key-value pairs through hashing, so on the surface it looks like a HashMap. The difference is that Hashtable is synchronised: every method is locked, which makes it thread-safe for concurrent access. It is one of the oldest Java classes, dating back to JDK 1.0.
Because of that locking, Hashtable runs slower than HashMap. It also allows no null key and no null value, so putting a null throws a NullPointerException. Today it is considered legacy, and modern code prefers other options for thread safety.
Advantages of Hashtable:
- Thread-safe, since all its methods are synchronised.
- Safe for many threads sharing one table without extra code.
- Simple, well-understood behaviour from long use.
Disadvantages of Hashtable:
- Slower than HashMap, due to whole-table locking.
- Rejects null keys and null values, throwing an exception.
- Legacy design, largely replaced by ConcurrentHashMap.
HashMap vs Hashtable: Comparison Table

| Aspect | HashMap | Hashtable |
|---|---|---|
| Synchronisation | Not synchronised | Synchronised (thread-safe) |
| Null key / values | One null key and null values allowed | No null key or value (throws NullPointerException) |
| Performance (single thread) | Faster, no locking overhead | Slower due to synchronisation |
| Performance (multithread) | Needs external synchronisation | Built-in synchronisation |
| Iteration | Iterator, which is fail-fast | Iterator and legacy Enumeration |
| Introduced in | JDK 1.2 (Collections Framework) | JDK 1.0 (legacy) |
| Default capacity | 16, load factor 0.75 | 11, load factor 0.75 |
| Sizing | Resizes dynamically as it fills | Also resizes dynamically as it fills |
| Collision handling | Chaining (and treeify large buckets) | Chaining |
| Concurrency control | External, or use ConcurrentHashMap | Whole-table lock on each method |
| Preferred use | Single-threaded, general purpose | Rarely; kept for legacy code |
| Scenario suitability | Where synchronisation is not critical | Where simple thread safety is required |
| Modern usage | Common in modern Java | Legacy; alternatives available |
| Recommendation | Default map choice | Prefer ConcurrentHashMap instead |
How Hashing Works (Code)

Both classes share the same core idea: a hash function turns a key into a bucket index, so a lookup goes almost straight to the value. The clearest way to see their difference is the null-key rule.
import java.util.HashMap;
import java.util.Hashtable;
public class MapDemo {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put(null, 0); // HashMap allows one null key
System.out.println(map.get("a")); // 1
Hashtable<String, Integer> table = new Hashtable<>();
table.put("a", 1); // fine
// table.put(null, 0); // Hashtable would throw NullPointerException
System.out.println(table.get("a")); // 1
}
}
Notice the one commented line. A HashMap accepts null as a key, while a Hashtable rejects it at runtime. So that single behaviour often decides which class a codebase uses.
The Modern Choice: ConcurrentHashMap
Hashtable makes a whole map thread-safe, yet it does so with one coarse lock, so only one thread works at a time. That is why modern Java rarely uses it. Instead, when threads must share a map, developers reach for ConcurrentHashMap.
ConcurrentHashMap locks only small parts of the map, so many threads can read and write at once. As a result, it scales far better than Hashtable under load. So the practical rule is simple: use HashMap for single-threaded code, and ConcurrentHashMap for concurrent code, leaving Hashtable to old projects.
When to Use Which
You choose by asking whether threads share the map.
Use a HashMap for almost everything in single-threaded code. It is fast, it accepts a null key, and it is the modern default. So unless you have a concurrency need, reach for HashMap first.
Use a Hashtable only when you maintain legacy code that already depends on it. For new thread-safe needs, prefer ConcurrentHashMap, since it gives safety without the coarse lock. Therefore Hashtable is a compatibility choice rather than a fresh one.
Interview Questions
Frequently Asked Questions
Wrapping Up
HashMap and Hashtable both hash key-value pairs, yet they sit at different trade-off points. HashMap is fast, unsynchronised, and allows a null key, while Hashtable is synchronised, rejects null, and is a legacy class.
Remember the simple rule: use HashMap for single-threaded code, and reach for ConcurrentHashMap, not Hashtable, when threads share the map. Knowing the synchronisation and null differences answers most exam and interview questions on the two.
Related reading on DiffStudy: