Programs

Differences Between HashMap and HashTable in Java

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. 

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. 

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.

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. 

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. 

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

Become a Full Stack Developer

Executive PG Programme in Software Development with Specialization in Full Stack Development
Apply Now

0 replies on “Differences Between HashMap and HashTable in Java”

Accelerate Your Career with upGrad

Our Popular Software Engineering Courses

×