There are several differences between
Hashtable in Java:
Hashtableis synchronized, whereas
HashMapis not. This makes
HashMapbetter for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.
Hashtabledoes not allow
nullkeys or values.
nullkey and any number of
|1) HashMap is non synchronized. It is not-thread safe and can’t be shared between many threads without proper synchronization code.||Hashtable is synchronized. It is thread-safe and can be shared with many threads.|
|2) HashMap allows one null key and multiple null values.||Hashtable doesn’t allow any null key or value.|
|3) HashMap is a new class introduced in JDK 1.2.||Hashtable is a legacy class.|
|4) HashMap is fast.||Hashtable is slow.|
|5) We can make the HashMap as synchronized by calling this code|
Map m = Collections.synchronizedMap(hashMap);
|Hashtable is internally synchronized and can’t be unsynchronized.|
|6) HashMap is traversed by Iterator.||Hashtable is traversed by Enumerator and Iterator.|
|7) Iterator in HashMap is fail-fast.||Enumerator in Hashtable is not fail-fast.|
|8) HashMap inherits AbstractMap class.||Hashtable inherits Dictionary class.|
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.