Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Differences Between HashMap and HashTable in Java

By Rohan Vats

Updated on Jun 16, 2023 | 8 min read

Share:

If you’re learning about Java and its applications, you must have come across HashMap and HashTable. Both of them are among the most important classes in the Java Collection framework. You’ll be using them extensively while developing with Java, which is why it’s vital to understand their differences. 

Check out our free courses to get an edge over the competition

In the following points, we’ll cover the HashTable vs HashMap topic in detail and explain the distinctions between the two: 

What is HashMap?

HashMap has been in Java’s collection since the introduction of Java 1.2. It allows you to perform basic implementations of the Map interface of Java. HashMap stores data in (Key, Value) pairs, and to access them, you’ll have to use an index of another type, such as an integer. 

Here, you use one object as a key (index) for another object (value), hence the use of (key, value) pair. If you add a duplicate key, it will replace the element of the corresponding key. 

Check out upGrad’s Advanced Certification in Blockchain

Features of HashMap

To understand the distinctions between HashTable vs HashMap, first, you must understand their features. It would make you familiar with the basics of both of them. HashMap has the following features:

  • It is part of java.util package.
  • It extends an abstract class AbstractMap that provides an incomplete implementation of the Map interface.
  • Along with implementing Map interface, it also implements Serializable and Cloneable interfaces. 
  • It allows duplicate values but doesn’t allow duplicate keys. This means one key can’t have more than one value, but multiple keys can have a single value. 
  • You can use the null key only once in a HashMap, but you can use multiple null values. 
  • It makes no guarantees about the order of the map, particularly whether or not the order would remain constant over time. HashMap is nearly similar to HashTable but is unsynchronised. 
  • HashSet uses HashMap internally.

Check out upGrad’s Advanced Certification in DevOps 

What is HashTable?

You use the HashTable class to implement a hash table that maps keys to values. Here, you can use non-null objects as a key or as a value. Keep in mind that to store and retrieve objects from a hashtable successfully, the objects you used as keys must implement the hashCode method and the equals method. 

A hashtable stores data in an array format, and every data value has a unique index value. This allows you to access particular data quite quickly if you know the required index. 

Features of HashTable

HashTable has its peculiar features, just like HashMap. However, these features make it unique and different from HashMap in many respects: 

  • HashTable is quite similar to HashMap but is synchronised.
  • It stores key-value pairs in a hash table.
  • Here, you specify an object used as a key and the value you want to associate to the same. Then you hash the key and use the generated hash code as the index where you store the value within the table. 
  • The default capacity of the Hashtable class is 11, and the load factor is 0.75.
  • HashMap doesn’t offer Enumeration, whereas Hashtable offers not fail-fast Enumeration. 

Similarities in HashMap and HashTable

We may better understand the difference between HashMap and HashTable after we are familiar with fundamental ideas and the similarities between HashMaps and Hash Tables.

  • In Java, neither HashMap nor HashTable maintains insertion order.
  • The Java.util.Map interface is implemented by both HashMap and HashTable.
  • The complexity offered by methods of both HashTable and HashMap is the same in Java:
Operation Worst Case Best Case
put(K key, V value) O(n) O(1)
get(Object key) O(n) O(1)

Differences Between HashMap and HashTable

Now that you know the particular highlights of HashMap and HashTable, we’ll now compare them and see the prominent differences between the two:

HashMap HashTable
It is non-synchronised. You can’t share many threads here without using proper synchronisation code because it is not thread-safe.  It is synchronised. You can share it with many threads because it is thread-safe.
It inherits the AbstractMap class. It inherits the Dictionary class. 
Here, the Iterator is fall-fast.  In HashTable, the Enumerator is not fall-fast.
The Iterator traverses the HashMap. The Enumerator and Iterator traverse the HashTable. 
You can make a HashMap synchronised by calling the code
Map m = Collections.synchronisedMap(hashMap);
Hash Tables are synchronised internally, and you can’t unsynchronised them with any code. 
It is quite fast.  It is relatively slower than HashMap.
HashMap is a new class and has been recently introduced in JDK 1.2.  HashTable is a legacy class. 
It allows multiple null values and one null key.  It doesn’t allow any null values or key. 

Coding Example

Following is an example of HashMap and HashTable at work so you can understand the distinctions of HashMap vs HashTable. 

Input: 
import java.util.*;

import java.lang.*;

import java.io.*;

public class JavaTester{

   public static void main(String args[]){

      Hashtable ht=new Hashtable();

      ht.put(1,”Uday”);

      ht.put(1,”Ujjwal”);

      ht.put(2,”Sumit”);

      ht.put(3,”Vijay”);

      System.out.println(“————-Hash table————–“);

      Set<Integer> keySet = ht.keySet();

      for (Integer key:keySet) {

         System.out.println(key + ” “+ht.get(key));

      }

      HashMap hm=new HashMap();

      hm.put(0,”Uday”);

      hm.put(4,”Uday”); // you can have duplicate values in a hashmap

      hm.put(1,”Sumit”);

      hm.put(2,”Vijay”);

      System.out.println(“———–Hash map———–“);

      Set<Integer> keySet1 = ht.keySet();

      for (Integer key:keySet) {

         System.out.println(key + ” “+hm.get(key));

      }

   }

}

Output: 

Hash table: 

3 Vijay

2 Sumit

1 Ujjwal

Hash map:

0 Uday

1 Sumit

2 Vijay

4 Uday

When to use HashMap vs HashTable?

The primary factor that determines whether you’ll use HashMap or HashTable is synchronisation. If you need a thread-safe task, then you should use HashTable because all its methods are synchronised. However, it is a legacy class, and you should avoid them.

If you have a multithreaded environment, you should use ConcurrentHashMap as it’s pretty similar to HashTable. It allows you to make the HashMap synchronised properly.

Synchronised operations cause poor performance, so you should avoid them in most cases. Moreover, HashMap is suitable for a non-threaded environment, so you can use it easily. 

Learn Software Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Concurrent HashMap vs HashTable

ConcurrentHashMap uses HashTable as its basic data structure. But there still exist a few differences to be noted:

Concurrent HashMap HashTable
While adding or updating the map, Concurrent HashMap only applies locks at the fragment bucket level. The entire collection is locked using a HashTable.
Over a HashTable, it is superior. The entire collection is locked using a HashTable.

Hashmap vs Hashtable: Limitations

When discussing the differences, you must also be familiar with their limitations which can help you choose the correct data structure in Java.

Limitations of HashMap

  • The HashMap performs poorly in case of a chance of collision when two distinct keys produce the same hashCode() value.
  • Sometimes HashMap needs to be resized since the buckets’ original size is full. The elements from the old HashMap are transferred to a new, larger HashMap, which takes O(n) time.

Limitations of Hash table

  • The data to be read is distributed in-memory seemingly at random, which is a problem with HashTables in general.
  • Databases may deteriorate if there are a lot of collisions, which is one of the drawbacks of HashTables. The more data, the greater the chances of collision. Many hash algorithms lack the ability to switch from one data set to another or to the previous data set.

Conclusion

HashMap and HashTable are popular Java codes with similar functions. However, as you can see there are several prominent differences between the two. HashMap is a Java class, whereas HashTable is a data structure. 

If you’re interested in learning more about Java and other programming languages, we recommend checking our Executive PG Programme in Software Development with Specialization in Full Stack Development

Frequently Asked Questions (FAQs)

1. What is a hash table?

2. What is collections framework in Java?

3. What is the relation between a hashtable and hashmap in Java?

4. When would a HashTable be preferable over a HashMap?

5. HashMap is ideal for non-threading applications if your application does not require any multi-threading tasks.

Rohan Vats

419 articles published

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

India’s #1 Tech University

Executive PG Certification in AI-Powered Full Stack Development

77%

seats filled

View Program

Top Resources

Recommended Programs

Suggested Blogs