Skip to content
Related Articles

Related Articles

Improve Article
Difference between HashMap and HashSet
  • Difficulty Level : Easy
  • Last Updated : 30 Sep, 2019

HashSet is implementation of Set Interface which does not allow duplicate value. The main thing is, objects that are stored in HashSet must override equals() for check for equality and hashCode() methods for no duplicate value are stored in our set.

HashMap is an implementation of Map Interface, which map a key to value. Duplicate keys are not allowed in a map.Basically Map Interface has two implementation classes HashMap and TreeMap the main difference is TreeMap maintains order of the objects but HashMap will not.HashMap allows null values and null keys.

Both HashSet and HashMap are not synchronized.

Difference between HashMap and HashSet

1) Implementation: HashMap implements Map interface and HashSet implements Set interface.



2) Duplicates: HashSet does’t allow duplicate values. HashMap store key, value pairs and it does not allow duplicate keys. If key is duplicate then old key is replaced with new value.

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

4) Dummy value : In HashMap no concept of dummy value,
HashSet internally uses HashMap to add elements. In HashSet, the argument passed in add(Object) method serves as key K. Java internally associates dummy value for each value passed in add(Object) method.

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

6) Faster:HashSet is slower then HashMap,

7) Insertion HashMap use put() method for storing data, While in HashSet use add() method for add or storing data.

8) Example: HashSet is a set, e.g. {1, 2, 3, 4, 5, 6, 7},

HashMap is a key -> value pair(key to value) map, e.g. {a -> 1, b -> 2, c -> 2, d -> 1}



Notice in my example above that in the HashMap there must not be duplicate keys, but it may have duplicate values.

In the HashSet, there must be no duplicate elements

Example of HashSet and HashMap in Java

HashSet




// Java program to demonstrate working of HashSet
import java.util.HashSet;
  
public class HashSetExample {
  
    public static void main(String[] args)
    {
  
        HashSet<String> hs = new HashSet<String>();
        // Adding elements to the HashSet
        hs.add("geeks");
        hs.add("practice");
        hs.add("contribute");
;
  
        System.out.println("Before adding duplicate values \n\n" + hs);
  
        // Addition of duplicate elements
        hs.add("geeks");
        hs.add("practice");
  
        System.out.println("\nAfter adding duplicate values \n\n" + hs);
  
        // Addition of null values
        hs.add(null);
        hs.add(null);
  
        // Displaying HashSet elements
        System.out.println("\nAfter adding null values \n\n" + hs);
    }
}
Output:
Before adding duplicate values 

[practice, geeks, contribute]

After adding duplicate values 

[practice, geeks, contribute]

After adding null values 

[null, practice, geeks, contribute]

HashMap




import java.util.HashMap;
  
public class HashMapExample {
  
    public static void main(String[] args)
    {
  
        /* This is how to declare HashMap */
        HashMap<Integer, String> hm = new HashMap<Integer, String>();
  
        /*Adding elements to HashMap*/
        hm.put(12, "geeks");
        hm.put(2, "practice");
        hm.put(7, "contribute");
  
        System.out.println("\nHashMap object output :\n\n" + hm);
  
        // store data with duplicate key
        hm.put(12, "geeks");
  
        System.out.println("\nAfter inserting duplicate key :\n\n" + hm);
    }
}
Output:
HashMap object output :

{2=practice, 7=contribute, 12=geeks}

After inserting duplicate key :

{2=practice, 7=contribute, 12=geeks}

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :