Update appNew update is available. Click here to update.
Last Updated: Mar 5, 2023

Difference between HashMap and Hash Table in Java


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.2 Hashtable 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

You can also read about the Longest Consecutive Sequence.

Frequently Asked Questions

Why do we implement hashing in the data structure?

We use hashing to perform actions such as insertion, deletion, and search in constant time.

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);


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.  

Recommended Reading:

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on CodeStudio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, Basics of C++, Basics of Java, Basics of Python, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on CodeStudio.

Happy Coding!

Previous article
Hash Table vs STL Map
Next article
Difference between HashMap and TreeMap
Codekaze-June23 India's Biggest Tech Hiring Challenge is LIVE!
Register Now
Go on top