Introduction
The HashMap, Hashtable and HashSet are the classes of java.util package. This classes are the part of Java's collection framework. The HashMap class extends the AbstractMap class and implements the Serializable, Cloneable and Map interface. The Hashtable class extends Dictionary and implements Serializable and Cloneable interface. The HashSet extends AbstractSet and implements Serializable, Cloneable and Set interface.
Comparing HashMap vs Hashtable vs HashSet
The table below shows the similarity and differences between HashMap vs Hashtable vs HashSet.
HashMap | Hashtable | HashSet |
---|---|---|
It is a hash table based implementation of Map interface. | This class implements a hash table, which maps keys to values. | It is a hash table based implementation of Set interface. |
It stores data as a key-value pair. | It stores data as a key-value pair. | It stores data as an objects. |
It does not allow duplicate keys but duplicate values are allowed. | It does not allow duplicate keys but duplicate values are allowed. | It does not allow duplicate values. |
It allows one null key and multiple null values | It does not allow any null key or value. | It can contain a single null value |
HashMap is non synchronized class. | Hashtable is synchronized class. | HashSet is non Synchronized class. |
It is faster than Hashtable and HashSet. | It is not faster than HashMap. | It is not faster than HashMap. |
We can traverse HashMap using the iterator. | We can traverse Hashtable using enumerator and iterator both. | We can traverse HashSet using the iterator. |
The put() method is used for inserting values. |
The put() method is used for inserting values. |
The add() method is used to insert the elements in HashSet. |
Some examples to demonstrate the comparison
Example 1 : Operations on HashMap
Here, we will demonstrate the operations on HashMap. It provides the basic implementation of the Map interface of Java. It stores the data in (Key, Value) pairs, and we can access them by an index of another type. The HashMap is asynchronized implementation of hashing. So, this is faster as compared to the Hashtable. In this example, we are adding some key value pair to the hashmap. Thereafter, we are getting the size, retrieving the value, and checking for the presence of the key in the hashmap.
// Program to demonstrate HashMap vs Hashtable vs HashSet
import java.util.HashMap;
public class Main
{
public static void main(String[] args)
{
HashMap <String,Integer> h= new HashMap<String, Integer>();
// Add elements to the hashmap
h.put("Computer", 151);
h.put("Information Technology", 225 );
h.put("Electrical", 281);
h.put("Civil", 196);
System.out.println("HashMap is ");
System.out.println(h);
// replace the Computer's code
h.put("Computer", 125);
// adding duplicate keys
h.put("Civil", 196);
System.out.println("After updating the value and adding duplicate keys: ");
System.out.println(h);
// Print size and content
System.out.println("Size is:- " + h.size());
System.out.println(h);
// Checking for the presence of key
System.out.println("Does hashmap contains Computer? "+h.containsKey("Computer"));
System.out.println("Does hashmap contains computer? "+h.containsKey("computer"));
// Printing the value for the key
System.out.println("value for key Electrical is "+h.get("Electrical"));
}
}
Output
HashMap is
{Civil=196, Computer=151, Electrical=281, Information Technology=225}
After updating the value and adding duplicate keys:
{Civil=196, Computer=125, Electrical=281, Information Technology=225}
Size is:- 4
{Civil=196, Computer=125, Electrical=281, Information Technology=225}
Does hashmap contains Computer? true
Does hashmap contains computer? false
value for key Electrical is 281
Example 2 : Operations on Hashtable
Here, we will demonstrate the operations on Hashtable class. The Hashtable class of java.util package implements a hash table that maps keys to values. Any non null object can be used as a key or as a value. Moreover, the objects used as keys must implement the hashCode method and the equals method. This is a synchronized implementation of hashing. In this example, we are adding key value pair to the hash table. Thereafter, we are getting the size, retrieving the value, and checking for the presence of the key in the hashtable.
// Program to demonstrate HashMap vs Hashtable vs HashSet
import java.util.Hashtable;
public class Main {
public static void main(String args[]) {
// creating object of Hashtable
Hashtable < String, Integer > h = new Hashtable < String, Integer > ();
// adding key-value pair
h.put("Computer", 151);
h.put("Information Technology", 225);
h.put("Electrical", 281);
h.put("Civil", 196);
System.out.println("Hashtable is ");
System.out.println(h);
//replace the Computer's code
h.put("Computer", 125);
// adding duplicate value
h.put("Civil1", 196);
System.out.println("After updating the value and adding duplicate keys: ");
System.out.println(h);
// Print size and content
System.out.println("Size of hash table is:- " + h.size());
System.out.println(h);
// Checking for the presence of key
System.out.println("Does hashtable contains Computer? " + h.containsKey("Computer"));
System.out.println("Does hashtable contains computer? " + h.containsKey("computer"));
// Printing the value for the key
System.out.println("value for key Electrical is " + h.get("Electrical"));
}
}
Output
Hashtable is
{Information Technology=225, Civil=196, Electrical=281, Computer=151}
After updating the value and adding duplicate keys:
{Information Technology=225, Civil=196, Electrical=281, Computer=125, Civil1=196}
Size of hash table is:- 5
{Information Technology=225, Civil=196, Electrical=281, Computer=125, Civil1=196}
Does hashtable contains Computer? true
Does hashtable contains computer? false
value for key Electrical is 281
Example 3 : Operations on HashSet
Here, we will demonstrate the operations on HashSet class. The HashSet class of java.util package implements the Set interface, backed by a hash table which is actually a HashMap instance. The class does not guarantee the constant order of elements over time. Moreover, this implementation is not synchronized. So, if multiple threads access a hash set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. In this example, we are adding values to the HashSet. Thereafter, we are getting the size, retrieving the value, and checking for the presence of the key in the HashSet.
// Program to demonstrate HashMap vs Hashtable vs HashSet
import java.util.*;
public class Main {
public static void main(String args[]) {
// creating object of HashSet
HashSet h = new HashSet();
// adding key-value pair
h.add("Computer");
h.add("Information Technology");
h.add("Electrical");
h.add("Civil");
System.out.println("HashSet is ");
System.out.println(h);
// adding null value
h.add(null);
// adding duplicate keys
h.add("Civil");
System.out.println("After updating the value and adding duplicate keys: ");
System.out.println(h);
// Print size and content
System.out.println("Size is:- " + h.size());
Iterator i = h.iterator();
while (i.hasNext())
System.out.println(i.next());
// Checking for the presence of key
System.out.println("Does HashSet contains Computer? " + h.contains("Computer"));
}
}
Output
HashSet is
[Civil, Computer, Electrical, Information Technology]
After updating the value and adding duplicate keys:
[null, Civil, Computer, Electrical, Information Technology]
Size is:- 5
null
Civil
Computer
Electrical
Information Technology
Does HashSet contains Computer? true
Summary
In this tutorial, we covered the major difference between HashMap vs Hashtable vs HashSet. As per the requirement of an application, we can choose the synchronized or asynchronized version of hash class to store the key value pairs.
We learned in detail about the operations and types of values allowed with an example. All in all, this tutorial, covers everything that you need to know in order to have a clear view on difference between HashMap vs Hashtable vs HashSet Java.
References
Hashtable Class
HashMap Class
HashSet Class