In Java, the “HashMap” and “HashSet” act as a container to contain the data appropriately. It is such that these approaches come into effect while saving the records directly or as “key-value” pairs. These approaches make this procedure convenient as these enable the developer to append, update and delete the data conveniently.
This guide will demonstrate the differences between “HashMap” and “HashSet” in Java.
What is the Difference Between HashMap and HashSet in Java?
“HashMap” is a Java functionality that is used to input the values as “key-value” pairs and can have duplication in the values. On the other hand, the “HashSet” can be utilized to input a single value at a time and it cannot have duplicates.
Following are the core differences between both the discussed functionalities:
HashMap | HashSet |
---|---|
It implements the Map interface. | It implements the Set interface. |
It comprises a single null key and can have any number of null values. | It has a single null key. |
It can have duplicate values (not the keys). | It cannot have duplicates. |
HashSet is comparatively faster than HashSet | HashSet is slower than HashMap. |
The entries are contained as key-value pairs. | The entries are contained as objects. |
Its complexity is O(1). | Its complexity is O(n). |
Two values are needed to be inserted as key-value pairs. | Only a single value can be added to it at a time. |
Include the below-given package in both the examples of this guide to invoke all the features in the “java.util” package:
import java.util.*;
Example 1: Working of “HashMap” in Java
This example demonstrates the working of “HashMap” in Java:
public class Hashmapset {
public static void main(String[] args){
HashMap<Integer, String> x = new HashMap<Integer, String>();
x.put(1, "Java");
x.put(2, "Programming");
x.put(3, "Language");
System.out.println("HashMap Values -> " + x);
x.put(2, "Java");
System.out.println("\nAfter Adding Duplicate Value \n" + x);
x.put(4, null);
x.put(5, null);
System.out.println("\nAfter Adding Null Values \n" + x);
}}
According to the above code lines:
- Create a HashMap object using the “new” keyword and the “HashMap()” constructor. This HashMap supports the keys as “Integers” and values as “Strings”.
- Now, add the stated “key-value” pairs in it via the “put()” method.
- After that, append a duplicate key and value already contained in the HashMap.
- Also, insert two “null” values in the HashMap against the provided keys.
Output

In this output, it can be verified that the duplicate value (not the key) is appended to the HashMap, and the specified “null” values are also appended appropriately.
Example 2: Working of HashSet in Java
In this specific demonstration, the working of “HashSet” can be analyzed:
public class Hashmapset {
public static void main(String[] args){
HashSet<String> x = new HashSet<String>();
x.add("Java");
x.add("Programming");
x.add("Language");
System.out.println("HashSet Values -> " + x);
x.add("Java");
System.out.println("\nAfter Adding Duplicate Values \n" + x);
x.add(null);
x.add(null);
System.out.println("\nAfter Adding Null Values \n" + x);
}}
In this block of code:
- Create a HashSet of “String” type (accepts only 1 value at a time).
- Now, insert the stated string values in it via the “add()” method.
- Likewise, append a duplicate value and two “null” values.
- Finally, display the final “HashSet”.
Output

In this outcome, it can be implied that the duplicate value is not supported in this case and only a single “null” value is appended as discussed.
Therefore, a clear difference can be extracted from the generated output in both the cases of “HashMap” and “HashSet”.
Conclusion
In Java, “HashMap” inputs the values as “key-value” pairs and can have duplication in the values whereas the “HashSet” can be utilized to input a single value at a time and cannot have duplicates. This guide demonstrated the core differences between both the HashMap and HashSet in Java with examples.