HashMap vs Hashtable

Difference between HashMap and Hashtable

HashMap Hashtable
1) HashMap is non synchronized. which means multiple threads can work on HashMap at the same time. Hashtable is synchronized. which means only one threads can work on HashMap at the same time.
2) HashMap allows one null key and multiple null values. Hashtable doesn’t allow null keys and null values.
3) HashMap class  is  introduced in JDK 1.2. Hashtable is a legacy class.
4)Hashmap is much faster and uses less memory than Hashtable as former is unsynchronized. Hashtable is slow.
5) HashMap can be synchronized by

Map m = Collections.synchronizedMap(hashMap);

Hashtable is internally synchronized and can’t be unsynchronized.
6) Hashmap object values are iterated by using iterator Hashtable object values are iterated Enumerator as well as Iterator.
7) Iterator in HashMap is fail-fast. Enumerator in Hashtable is not fail-fast.
8) HashMap inherits AbstractMap class. Hashtable inherits Dictionary class.

 

Similarities Between HashMap and Hashtable

  1. Both HashMap and Hashtable implements Map interface .
  2. Both HashMap and Hashtable  does not guarantee that  the order of the map will remain constant over time.
  3. Both HashMap and Hashtable provides constant time performance for put and get methods .
  4. Both HashMap and Hashtable works on the Principle of Hashing.

One thought on “HashMap vs Hashtable

Leave a comment