What is difference between HashMap and Hashtable?

  1. HashMap is not synchronized.

    Hashtable is synchronized.

  2. HashMap can contain one null key and multiple null values.

    Hashtable cannot contain any null key nor value.

1)HashMap lets you have null values as well as one null key.

1)HashTable does not allows null values as key and value.

2)The iterator in the HashMap is fail-safe (If you change the map while iterating, you’ll know).

2)The enumerator for the Hashtable is not fail-safe.

3)HashMap is unsynchronized.

3)Hashtable is synchronized.

Note: Only one NULL is allowed as a key in HashMap. HashMap does not allow multiple keys to be NULL. Nevertheless, it can have multiple NULL values.

HashMap Hashtable

HashMap object is not synchronized by default. Hashtable object is synchronized by default. In case of a single thread, using HashMap is faster than the Hashtable. In case of multiple threads, using Hashtable is advisable, with a single thread, Hashtable becomes slow. HashMap allows null keys and null values to be stored Hashtable does not allow null keys or values. Iterator in the HashMap is fail-fast. This means Iterator will produce exeception if concurrent updates are made to the HashMap. Enumeration for the Hashtable is not fail-fast. This means even if concurrent updations are done to Hashtable, there will not be any incorrect results produced by the Enumeration.