HashMap vs Hashtable in Java
- Hashtable is a legacy class β don't use it in new code. Use HashMap for single-threaded access, ConcurrentHashMap for multi-threaded.
- HashMap allows one null key and multiple null values. Hashtable and ConcurrentHashMap throw NullPointerException for null keys or values.
- ConcurrentHashMap is the modern replacement for Hashtable β it's thread-safe and significantly faster because it only locks segments rather than the entire map.
Hashtable predates the Java Collections Framework (it's been around since Java 1.0). It's a legacy class. If you're writing new code and thinking about thread-safety in a Map, the answer is ConcurrentHashMap, not Hashtable. HashMap for single-threaded code, ConcurrentHashMap for concurrent code. Hashtable for legacy code you're maintaining.
Key Differences with Code Examples
package io.thecodeforge.collections; import java.util.HashMap; import java.util.Hashtable; import java.util.concurrent.ConcurrentHashMap; public class HashMapVsHashtableExample { public static void main(String[] args) { // HashMap β not thread-safe, allows null key and null values HashMap<String, String> hashMap = new HashMap<>(); hashMap.put(null, "null key allowed"); hashMap.put("key1", null); // null value allowed hashMap.put("key2", "value2"); System.out.println(hashMap.get(null)); // null key allowed // Hashtable β thread-safe, does NOT allow null key or null value Hashtable<String, String> hashtable = new Hashtable<>(); try { hashtable.put(null, "value"); // Throws NullPointerException } catch (NullPointerException e) { System.out.println("Hashtable: null key throws NPE"); } hashtable.put("key1", "value1"); // Fine // ConcurrentHashMap β thread-safe, better performance than Hashtable // Does NOT allow null key or null value (same as Hashtable) ConcurrentHashMap<String, String> concurrentMap = new ConcurrentHashMap<>(); concurrentMap.put("service", "PaymentService"); // concurrentMap.put(null, "x"); // NullPointerException System.out.println("HashMap size: " + hashMap.size()); System.out.println("Hashtable size: " + hashtable.size()); System.out.println("ConcurrentHashMap size: " + concurrentMap.size()); } }
Hashtable: null key throws NPE
HashMap size: 3
Hashtable size: 1
ConcurrentHashMap size: 1
| Feature | HashMap | Hashtable | ConcurrentHashMap |
|---|---|---|---|
| Thread-safe? | No | Yes (fully synchronised) | Yes (segment-level) |
| Null keys allowed? | Yes (one) | No | No |
| Null values allowed? | Yes | No | No |
| Performance | Fast | Slow (full sync overhead) | Fast (concurrent reads) |
| Introduced | Java 1.2 | Java 1.0 (legacy) | Java 5 |
| Recommended for | Single-threaded code | Legacy code only | Multi-threaded code |
π― Key Takeaways
- Hashtable is a legacy class β don't use it in new code. Use HashMap for single-threaded access, ConcurrentHashMap for multi-threaded.
- HashMap allows one null key and multiple null values. Hashtable and ConcurrentHashMap throw NullPointerException for null keys or values.
- ConcurrentHashMap is the modern replacement for Hashtable β it's thread-safe and significantly faster because it only locks segments rather than the entire map.
- For read-heavy concurrent workloads, ConcurrentHashMap excels β multiple threads can read simultaneously. Hashtable serialises all access.
β Common Mistakes to Avoid
- βUsing Hashtable for new multi-threaded code β use ConcurrentHashMap instead. Hashtable synchronises every method entirely, which creates a bottleneck. ConcurrentHashMap allows concurrent reads.
- βExpecting Hashtable to be safe for compound operations like check-then-put β even though methods are synchronised individually, two-method sequences are not atomic. Use ConcurrentHashMap's atomic methods like putIfAbsent().
- βPutting null keys into a map that might be iterated concurrently β both Hashtable and ConcurrentHashMap throw NPE for null keys. Only HashMap allows null keys.
Interview Questions on This Topic
- QWhat is the difference between HashMap and Hashtable in Java?
- QWhy is Hashtable not recommended for new concurrent Java code?
- QWhat is the difference between Hashtable and ConcurrentHashMap?
Frequently Asked Questions
What is the difference between HashMap and Hashtable in Java?
HashMap is not synchronised (not thread-safe) and allows null keys and values. Hashtable is fully synchronised (every method is synchronised) and does not allow null keys or values. Hashtable is a legacy class from Java 1.0. For new multi-threaded code, use ConcurrentHashMap instead of Hashtable.
Is Hashtable deprecated in Java?
Hashtable is not formally deprecated but is considered a legacy class. The Java Collections Framework (introduced in Java 1.2) provides better alternatives: HashMap for non-concurrent use, ConcurrentHashMap for concurrent use. Modern code should not use Hashtable.
Developer and founder of TheCodeForge. I built this site because I was tired of tutorials that explain what to type without explaining why it works. Every article here is written to make concepts actually click.