Java Interview Question

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

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.

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