Difference between HashMap and Hash Table in Java

Aditya Narayan Joardar
Last Updated: May 13, 2022


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.



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.2Hashtable is a legacy class.
An Iterator traverses hashMap.Enumerators and Iterator traverse hashtables.
HashMap inherits AbstractMap class.Hashtable inherits Dictionary class.


Java Implementation

import java.util.*;
import java.lang.*;

public class DiffHash {

  public static void main(String[] args)
      Hashtable<Integer,String> hashtable=new Hashtable<>();
      System.out.println("Hashtable Implementation");
      for (Map.Entry ht:hashtable.entrySet()) {
          System.out.println(ht.getKey()+" "+ht.getValue());

      HashMap<Integer,String> hashmap=new HashMap<>();

      System.out.println("HashMap Implementation");
      for (Map.Entry hm:hashmap.entrySet()) {
          System.out.println(hm.getKey()+" "+hm.getValue());


Hashtable Implementation
104 Sales
103 Accounts
102 Marketing
101 HR
HashMap Implementation
101 HR
102 Marketing
103 Accounts
104 Sales

Frequently Asked Questions

  1. Why do we implement hashing in the data structure?
    We use hashing to perform actions such as insertion, deletion, and search in constant time.
  2. Can we synchronize HashMaps in Java?
    Yes, HashMaps can be synchronized in Java using Collections. The statement to do so is
    Map map = Collections.sychronizeMap(hashMap);

Key Takeaways

To summarize the article, we learned about the difference between HashMap and Hashtable in Java. We first saw a small introduction to both, some differences, and Java implementations for both. We also discussed a few FAQs at the end.

Want to learn more about HashMap? Visit HashMap to know more.

Want to practice problems of different difficulty levels? Visit CodeStudio to know more.  

Happy Coding!

Was this article helpful ?


No comments yet

Be the first to share what you think