What is the difference between HashMap and Hashtable?
Updated 2026-07-10 · Beginner friendly
Quick answer
HashMap is not thread safe, allows one null key and many null values, and is faster, so it is the default for single threaded code. Hashtable is thread safe because its methods are synchronised, does not allow null keys or values, and is slower and largely legacy. For thread safe needs today, prefer ConcurrentHashMap over Hashtable.
Quick comparison
- Thread safety: HashMap is not synchronised, Hashtable is.
- Nulls: HashMap allows a null key and null values, Hashtable allows neither.
- Speed: HashMap is faster because it is not synchronised.
- Status: Hashtable is legacy, use ConcurrentHashMap for thread safe maps.
Map<String,Integer> m = new HashMap<>();
m.put(null, 1); // allowed in HashMap, not in Hashtable
In the interview
The modern answer beats the textbook one. Say use HashMap normally and ConcurrentHashMap when you need thread safety, and treat Hashtable as legacy. That shows you know current practice, not just history.
Common follow up questions
Related interview questions
Want the full Java guide?
Read every Java concept with notes, diagrams, and code in one place. Track your progress as you go.
Open the Java guide All Java questions