HashMap vs. HashTable

Main Difference

HashMap and HashTableare the two critical data structures in Java. They are in the Collection Framework having few things common in between the two. Although they have these similarities there are some differences between them. Both of the HashMap and HashTablestore the data in the form of key-value pairs and slao both implement the Map interface. For storing the elements the Hashing technique is being used by both of the HashMap and HashTable. As we have to address the differences between the two, so the most important difference is based upon the thread safety. Main points of difference between HashMap and HashTable include Null Keys, Iterating values, Synchronization. Performance and Class. HashTable is thread safe while on the other hand HashMap is not thread safe. HashTable is synchronized internally while HashMap is not synchronized internally, so HashMap is not safe to use in multi-threaded applications. The performance of HashMap is good as compared to HashTable as HashTable is synchronized internally. HashMap is preferred over the HashTable as HashTable is a legacy class and is considered as due to depreciation. HashMap is fast as compared to HashTable. As from the start of HashMap introduction in JDK 1.2 it is the member of Java Collection Framework, but before JDK 1.2 HashTable is already there and by the JDK 1.2 it has been made to implement Map interface and becoming the member of Collection Framework.

HashMap vs. HashTable — Is There a Difference?
ADVERTISEMENT

Difference Between HashMap and HashTable

HashMap vs. HashTable

HashTable is thread safe while on the other hand HashMap is not thread safe.

HashMap vs. HashTable

HashTable is synchronized internally while HashMap is not synchronized internally.

HashMap vs. HashTable

Both of the HashMap and HashTable implement the Map interface but they both extends different classes. HashMap implements the Map interface by extending AbstractMap class while on the other hand HashTable implements the Map interface by extending Dictionary class.

HashMap vs. HashTable

Maximum one null key is allowed by HashMap and any number of the null values is also allowed while HashTable neither allows a single null key nor the null value.

HashMap vs. HashTable

For traversing over the elements HashMap returns only Iterators while on the other hand HashTable returns are not only the Iterators but also the Enumeration.

HashMap vs. HashTable

Iterator returned by HasMap are fail-fast in nature while Enumeration returned by the HashTable are fail-safe in nature.

ADVERTISEMENT

HashMap vs. HashTable

HashMap is fast as compare to HashTable.

HashMap vs. HashTable

HashTable is slow as compare to HashMap.

HashMap vs. HashTable

Performance of HashMap is good as compared to HashTable as HashTable is synchronized internally.

HashMap vs. HashTable

HashMap is preffered over the HashTable as HashTable is a legacy class and is considered as due for depreciation.

HashMap vs. HashTable

As from the start of HashMap introduction in JDK 1.2 it is the member of Java Collection Framework but before JDK 1.2 HashTable are already there and by the JDK 1.2 it has been made to implement Map interface and becoming the member of Collection Framework.

Comparison Chart

HashMapHashTable
Maximum one null key is allowed by HashMap and any number of the null values is also allowed.Neither allows a single null key nor the null value.
Return
For traversing over the elements HashMap returns only IteratorsReturns are not only the Iterators but also the Enumeration.
Implementation
implements the Map interface by extending AbstractMap classImplements the Map interface by extending Dictionary class.
Sync
It is not synchronized internallyIt is synchronized internally
ADVERTISEMENT

Definition of HashMap

HashMap is the important data structure in the Collection Framework in Java. HashMap is not thread safe is not synchronized internally so HashMap is not safe to use in multi-threaded applications. But it can be safe for multi threaded applications by synchronizing it externally using Collections.synchronized() method. It implements the Map interface by extending AbstractMap class. Maximum one null key is allowed by HashMap and any number of the null values is also allowed. Iterator returned by HasMap are fail-fast in nature. Performance of HashMap is good and is thus fast as compare to the HashTable.

Definition of HashTable

HashTable is the important data structure in the Collection Framework in Java. HashTable is thread safe and is synchronized internally. Therefore it is safe to be used for multi-threaded applications. It implements the Map interface by extending Dictionary class. It neither allows a single null key nor the null value. HashTable is a legacy class and is considered as due to depreciation. Enumeration returned by the HashTable are fail-safe in nature.

Conclusion

Computer architecture is something that is a mystery for people who are general users and do not have a detailed knowledge of it. The two terms HashMap and HashTable are similar ones which are considered same but are different in the working and functions. This article, therefore, gives a proper understanding of the two types for people to get a clearer idea.