Difference between HashMap and HashSet - GeeksforGeeks

20 Apr, 2022
HashSet is an 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 maps 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 an order of the objects but HashMap will not.HashMap allows null values and null keys. Both HashSet and HashMap are not synchronized.
Now let us formulate the difference between HashMap and HashSet as provided in a tabular manner below as follows:
Let us grasp understanding by peeking into internal working with help of clean java programs.
Example 1: HashSet 
JAVA
// Java program to demonstrate working of HashSet // Importing HashSet class from java.util packageimport java.util.HashSet; // Mai classpublic class GFG {     // Main driver method    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);    }}
Before adding duplicate values 

[practice, geeks, contribute]

After adding duplicate values 

[practice, geeks, contribute]

After adding null values 

[null, practice, geeks, contribute]
Example 2: HashMap  
JAVA
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(7, "geeks");        hm.put(12, "contribute");         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=geeks, 12=contribute}
From the above two outputs after going through an understanding of their internal working, now we can talk about conceptual differences which are as follows:
Implementation: HashMap implements Map interface and HashSet implements Set interface.Duplicates: HashSet doesn’t allow duplicate values. HashMap stores key, value pairs and it does not allow duplicate keys. If the key is duplicate then the old key is replaced with the new value.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)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.Storing or Adding mechanism: HashMap internally uses hashing to store or add objects, HashSet internally uses HashMap object to store or add the objects.Speed: HashSet is slower than HashMap.Insertion HashMap uses the put() method for storing data, While in HashSet use add() method for add or storing data.
Implementation: HashMap implements Map interface and HashSet implements Set interface.
Duplicates: HashSet doesn’t allow duplicate values. HashMap stores key, value pairs and it does not allow duplicate keys. If the key is duplicate then the old key is replaced with the new value.
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)
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.
Storing or Adding mechanism: HashMap internally uses hashing to store or add objects, HashSet internally uses HashMap object to store or add the objects.
Speed: HashSet is slower than HashMap.
Insertion HashMap uses the put() method for storing data, While in HashSet use add() method for add or storing data.
Let us wrap up with an 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}
Here, in the example of the HashMap there must not be duplicate keys, but it may have duplicate values. In the HashSet, there must be no duplicate elements
solankimayank
madhavisharma2152
Java-Collections
Java-HashMap
java-hashset
Java-Map-Programs
Java-Set-Programs
Difference Between
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between BFS and DFS
Class method vs Static method in Python
Differences between TCP and UDP
Difference between var, let and const keywords in JavaScript
Difference between Process and Thread
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Reverse a string in Java
Arrays.sort() in Java with examples