Java Collections - Difference between HashMap and HashSet

Published April 11, 2022

When creating a unique set of values, you should definitely use HashSet. In Java, HashSets are used to implement the Set interface when duplicates are not permitted. HashMaps, on the other hand, are used to implement the Map interface. HashMaps support null values and value duplication. In this post, we will look at the key differences between the Hashmap and the HashSet.

Let's explore the differences between HashSet and HashMap.

 

Difference between HashMap and HashSet in Java

The HashMap and HashSet differ in various ways in Java. Let's look at these differences:

Implementation

First, the two implement different interfaces: HashSet implements the Set Interface while HashMaps implements the Map Interface.

 

Duplication

HashSets do not allow duplicate values, while Hashmaps allow duplication of values but no keys.

 Dummy Values

A HashSet supports dummy values, but a HashMap does not.

 

Objects required during the add operations

The HashSet requires one object, while the HashMaps requires two objects to carry out an add operation.

 

Mechanism of adding and Storing

The HashSet uses the HashMap object adding and storing mechanism, while the HashMap uses the Hashing technique to add and store values.

 

Method of Insertion

In HashSets, elements are inserted with the Add() method, while in HashMaps, elements are inserted with the Put() method.

 

When should you use a HashMap or a HashSet

When you do not want your data to be unique, hashMaps are the ideal option. However, when you want your data to be unique, hashSets are the ideal option.

 

Example of a HashMap and a HashSet

Here are examples of  HashMaps and HashSets

Example of HashMap

import java.util.HashMap;

public class HashMapSample {

            public static void main(String[] args) {

                        HashMap<Integer, String> heshmepu = new HashMap<Integer, String>();

                        heshmepu.put(2, "apples");

                        heshmepu.put(6, "oranges");

                        heshmepu.put(8, "kiwi");

                  System.out.println("\nHashMap items :\n\n" + heshmepu);

                  heshmepu.put(12, "Smith");

                  System.out.println("\n the Duplicate key :\n\n" + heshmepu);

            }

}

 

Example of HashSet

import java.util.HashSet;

public class HashSetSample {

            public static void main(String[] args) {

                        HashSet<String> hs = new HashSet<String>();

                  hs.add("kiwi");

                  hs.add("orange");

                  hs.add("melons");

                  System.out.println("Without Duplicate values \n\n" + hs);

                  hs.add("kiwi");

                  hs.add("orange");

                  System.out.println("\nWithuplicate values \n\n" + hs);

                  hs.add(null);

                  hs.add(null);

                  System.out.println("\nWith null values \n\n" + hs);

            }

}

 

Keywords: HashMap, HashSet

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

112 Views