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:
Basic | HashSet | HashMap |
---|---|---|
Implements | Set interface | Map interface |
Duplicates | No | Yes duplicates values are allowed but no duplicate key is allowed |
Dummy values | Yes | No |
Objects required during an add operation | 1 | 2 |
Adding and storing mechanism | HashMap object | Hashing technique |
Speed | It is comparatively slower than HashMap | It is comparatively faster than HashSet because of hashing technique has been used here. |
Null | Have a single null value | Single null key and any number of null values |
Insertion Method | Only one value is required for the insertion process. Add() function is used for insertion | Two values are required for the insertion process. Put() function is used for insertion. |
Data storage | The data is stored as objects. | The data is stored as key-value pair. |
Complexity | O(n) | O(1) |
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 package import java.util.HashSet; // Mai class public 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.
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}