Difference Between HashMap and HashTable

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

Comparison Chart

HashMapHashTable
DefinitionMaximum 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.
ReturnFor traversing over the elements HashMap returns only IteratorsReturns are not only the Iterators but also the Enumeration.
Implementationimplements the Map interface by extending AbstractMap classImplements the Map interface by extending Dictionary class.
SyncIt is not synchronized internallyIt is synchronized internally

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.

Differences in a Nutshell

  1. HashTable is thread safe while on the other hand HashMap is not thread safe.
  2. HashTable is synchronized internally while HashMap is not synchronized internally.
  3. 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.
  4. 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.
  5. 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.
  6. Iterator returned by HasMap are fail-fast in nature while Enumeration returned by the HashTable are fail-safe in nature.
  7. HashMap is fast as compare to HashTable.
  8. HashTable is slow as compare to HashMap.
  9. Performance of HashMap is good as compared to HashTable as HashTable is synchronized internally.
  10. HashMap is preffered over the HashTable as HashTable is a legacy class and is considered as due for depreciation.
  11. 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 Video

YouTube video

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.

Harlon Moss

Harlon currently works as a quality moderator and content writer for Difference Wiki. He graduated from the University of California in 2010 with a degree in Computer Science. Follow him on Twitter @HarlonMoss

View all posts by Harlon Moss