Introduction
Imagine you are working in an office. The office has many departments such as HR, Accounts, Sales, Marketing, etc. Each department has a unique intercom number associated with it. For example, HR can have extension 101; Sales can have extension 104, and so on.
In order to save all the extensions along with their corresponding departments, we need a special data structure. So in such cases, HashMaps and Hashtables come into play. Each of them has a special hash key and corresponding value.
But now the question arises are they similar data structures, what is the difference/benefit of using one over another? Well, this article is here to help you understand the differences between the two.
Notable differences between HashMap and Hashtable.
HashMap |
Hashtable |
It is unsynchronized and not thread-safe. | It is synchronized and thread-safe. |
HashMaps work fast. | Hashtables work slow. |
HashMap allows one null key and multiple null values. | Hashtables don't allow any null key or value. |
HashMap class was introduced in JDK -1.2 | Hashtable is a legacy class. |
An Iterator traverses hashMap. | Enumerators and Iterator traverse hashtables. |
HashMap inherits AbstractMap class. | Hashtable inherits Dictionary class. |