5 Difference between HashMap and HashSet in java with example

We have already discussed the difference between HashMap and Hashtable . Now in this post you will understand the difference between HashMap and HashSet classes . We will also look into the HashMap and HashSet example. We have already discussed HashMap internal mechanism , HashSet internal working in java .

Read Also :  Frequently asked Java Collections Interview Questions 

Difference between HashMap and HashSet 

1. Duplicates  :  HashSet does not allow duplicate values , in other words, adding a duplicate value leaves the HashSet object unchanged.

If the HashMap previously contain the mapping for the key, the old value is replaced. HashMap  does not allow duplicate keys. In other words, HashMap duplicate values can exist in HashMap.

2. Dummy value : There is no concept of dummy value in HashMap .

HashSet internally uses HashMap to add elements. In HashSet , the argument passed in add(Object) method serves as key K . we need to associate dummy value (automatically created by jdk) for each value  passed in add(Object) method.

3.  Implementation : HashMap implements Map interface, while HashSet implements Set interface.

4. Number of objects during add(put) operation :  HashMap requires two objects put(K key , V Value) to add an element to HashMap object.
HashSet requires only one object add(Object o) .

5.  Adding or Storing mechanism : HashMap internally uses hashing to add or store objects. HashSet internally uses HashMap object to add or store the objects.

Example of HashMap and HashSet in Java

import java.util.HashMap;
import java.util.HashSet;
public class HashMapHashSetExample { public static void main(String[] args) { 
        HashSet<String> hashsetobj = new HashSet<String>();
        hashsetobj.add("Alive is awesome");
        hashsetobj.add("Love yourself");
        System.out.println("HashSet object output :"+ hashsetobj);
        HashMap hashmapobj = new HashMap();
        hashmapobj.put("Alive is ", "awesome");  
        hashmapobj.put("Love", "yourself"); 
        System.out.println("HashMap object output :"+hashmapobj);   

Output :  HashSet object output :{Love yourself, Alive is awesome}
                 HashMap object output :{Alive is =awesome, Love=yourself}

Similarities between HashMap and HashSet 

1. Performance : Performance of  add,put,remove operations are almost same i.e constant time for HashSet and HashMap. It is incorrect to say HashMap is faster than HashSet.

2. Insertion Order :  Both HashMap and HashSet does not guarantee that the order will remain constant over time.

3. Unsynchronized : Both HashMap and HashSet implementation is unsynchronized.

4. Iterator type  : Iterator's returned by both (HashMap and HashSet) class's iterator and collection view methods are fail-fast.  find detailed explanation here fail-safe vs fail-fast in java.

When to use HashMap and HashSet in Java ?

difference between hashmap and hashset in java

The only time we need to prefer HashSet over HashMap when we are asked to maintain uniqueness in our collection object. Otherwise , always prefer HashMap over HashSet as HashSet is just a wrapper around HashMap.

Recap  : Difference between HashMap and HashSet in Java 

HashMap  HashSet            
DuplicatesYes, Duplicate values
but  no duplicate keys
Adding or Storing mechanism Hashing techniqueHashMap object          
Dummy valuesNoYes
Number Of Objects required
during add operation  

In case you have any other query or doubts regarding difference between HashMap and HashSet in Java then please mention in the comments .

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry