Difference Wiki

HashMap vs. HashTable

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.

Key Differences

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 is fast as compare to HashTable.
Janet White
Nov 12, 2016
HashTable is thread safe while on the other hand HashMap is not thread safe.
HashTable is synchronized internally while HashMap is not synchronized internally.
HashTable is slow as compare to HashMap.
Aimie Carlson
Nov 12, 2016
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.
ADVERTISEMENT
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.
Iterator returned by HasMap are fail-fast in nature while Enumeration returned by the HashTable are fail-safe in nature.
Performance of HashMap is good as compared to HashTable as HashTable is synchronized internally.
Harlon Moss
Nov 12, 2016
HashMap is preffered over the HashTable as HashTable is a legacy class and is considered as due for depreciation.
Janet White
Nov 12, 2016
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.
Janet White
Nov 12, 2016

Comparison Chart

.

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.
ADVERTISEMENT

Return

For traversing over the elements HashMap returns only Iterators
Returns are not only the Iterators but also the Enumeration.

Implementation

implements the Map interface by extending AbstractMap class
Implements the Map interface by extending Dictionary class.

Sync

It is not synchronized internally
It is synchronized internally
Aimie Carlson
Nov 12, 2016

HashMap and HashTable Definitions

HashMap

Alternative spelling of hash map

HashTable

Alternative spelling of hash table

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.

Trending Comparisons

New Comparisons