HashMap and the
Hashtable used to store data in key and value form. Both are using the hashing technique to store unique keys. But there are many differences between HashMap and Hashtable classes that are given below.
HashMap is non-synchronized. It is not-thread safe and can’t be shared between many threads without proper synchronization code.
HashMap allows one null key and multiple null values.
HashMap is a new class introduced in JDK 1.2.
HashMap is fast.
5) We can make the
HashMap as synchronized by calling this code
Map m = Collections.synchronizedMap(HashMap);
HashMap is traversed by Iterator.
7) Iterator in
HashMap is fail-fast.
HashMap inherits AbstractMap class.
Hashtable is synchronized. It is thread-safe and can be shared with many threads.
Hashtable doesn’t allow any null key or value.
Hashtable is a legacy class.
Hashtable is slow.
Hashtable is internally synchronized and can’t be unsynchronized.
Hashtable is traversed by Enumerator and Iterator.
7) Enumerator in
Hashtable is not fail-fast.
Hashtable inherits Dictionary class.
Further reading What is difference between HashMap and Hashtable in Java?