Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Notable differences between HashMap and Hashtable.
3.
Java Implementation
4.
Frequently Asked Questions
4.1.
Why do we implement hashing in the data structure?
4.2.
Can we synchronize HashMaps in Java?
5.
Conclusion
Last Updated: Mar 27, 2024

Difference between HashMap and Hash Table in Java

Introduction

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.

HashMap

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<>();
      hashtable.put(101,"HR");
      hashtable.put(104,"Sales");
      hashtable.put(102,"Marketing");
      hashtable.put(103,"Accounts");
      System.out.println("Hashtable Implementation");
      for (Map.Entry ht:hashtable.entrySet()) {
          System.out.println(ht.getKey()+" "+ht.getValue());
      }

      HashMap<Integer,String> hashmap=new HashMap<>();
      hashmap.put(104,"Sales");
      hashmap.put(103,"Accounts");
      hashmap.put(101,"HR");
      hashmap.put(102,"Marketing");

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


OUTPUT

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

Conclusion

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 Coding Ninjas Studio 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 Coding Ninjas Studio.

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 Coding Ninjas Studio.


Happy Coding!

Live masterclass