Differences Between HashMap and HashTable in Java
Updated on Jun 16, 2023 | 8 min read | 6.0k views
Share:
For working professionals
For fresh graduates
More
Updated on Jun 16, 2023 | 8 min read | 6.0k views
Share:
Table of Contents
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:
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
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:
Check out upGrad’s Advanced Certification in DevOps
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.
HashTable has its peculiar features, just like HashMap. However, these features make it unique and different from HashMap in many respects:
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.
Operation | Worst Case | Best Case |
put(K key, V value) | O(n) | O(1) |
get(Object key) | O(n) | O(1) |
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. |
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
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.
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. |
When discussing the differences, you must also be familiar with their limitations which can help you choose the correct data structure in Java.
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.
Get Free Consultation
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
Top Resources