A brief analysis of the differences between HashMap and Hashtable in Java

  • 2020-04-01 03:42:35
  • OfStack

HashMap is a lightweight (non-thread-safe) implementation of Hashtable, and they both complete the Map interface. The main difference is that HashMap allows null keys, which may be more efficient than Hashtable due to non-thread-safe.

A HashMap allows null as an entry key or value, while a Hashtable does not.

HashMap removes the contains method from the Hashtable and changes it to containsvalue and containsKey. Because the contains method is misleading.

Hashtable is inherited from the Dictionary class, and HashMap is an implementation of the Map interface introduced in Java1.2.

The big difference is that the method for Hashtable is Synchronize, while a HashMap is not, because instead of synchronizing its methods themselves when multiple threads access the Hashtable, a HashMap must provide external synchronization.

Both Hashtable and HashMap use roughly the same hash/rehash algorithm, so there isn't much difference in performance.


Related articles: