Difference between HashMap and HashSet
Last Updated :
06 Apr, 2023
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);
}
}
OutputBefore 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}
Similar Reads
Set in Java
The Set Interface is present in java.util package and extends the Collection interface. It is an unordered collection of objects in which duplicate values cannot be stored. It is an interface that implements the mathematical set. This interface adds a feature that restricts the insertion of duplicat
14 min read
AbstractSet Class in Java
In Java, the AbstractSet class is part of the Java Collections Framework. It provides a Skeleton implementation of the set interface, which is a collection that does not allow duplicate elements. This class is abstract, meaning it cannot be instantiated directly, but it can be extended to create a c
8 min read
EnumSet in Java
In Java, the EnumSet is a specialized set implementation for use with enum types. It is a part of java.util package and provides a highly optimized set for storing enum constants. The EnumSet is one of the specialized implementations of the Set interface for use with the enumeration type.It extends
9 min read
Java HashSet
HashSet in Java implements the Set interface of Collections Framework. It is used to store the unique elements and it doesn't maintain any specific order of elements. Can store the Null values.Uses HashMap (implementation of hash table data structure) internally.Also implements Serializable and Clon
12 min read
TreeSet in Java
TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree(red - black tree) for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. This must be consistent with equ
13 min read
ConcurrentSkipListSet in Java
In Java, the ConcurrentSkipListSet is the part of the java.util.concurrent package and provides a scalable, thread-safe alternative to TreeSet. It is a sorted set that lets multiple threads safely access and modify the set at the same time without causing issues.It is thread-safe.Elements are in sor
7 min read
CopyOnWriteArraySet in Java
In Java, the CopyOnWriteArraySet is the part of the java.util.concurrent package and is used to handle thread-safe operations in multi-threaded environments. It is ideal when the set is frequently read but infrequently modified. The set ensures safe access for multiple threads, as it creates a new c
6 min read
Java LinkedHashSet
LinkedHashSet in Java implements the Set interface of the Collection Framework. It combines the functionality of a HashSet with a LinkedList to maintain the insertion order of elements. Stores unique elements only.Maintains insertion order.Provides faster iteration compared to HashSet.Allows null el
8 min read
Convert HashSet to TreeSet in Java
Hashset: Hashset in Java is generally used for operations like search, insert and delete. It takes constant time for these operations on average. HashSet is faster than TreeSet. HashSet is Implemented using a hash table. TreeSet: TreeSet in Java takes O(log n) for search, insert and delete which is
3 min read
Difference and similarities between HashSet, LinkedHashSet and TreeSet in Java
In this article, we will learn, the difference between HashSet vs LinkedHashSet and TreeSet And similarities between LinkedHashSet and TreeSet. HashSet, LinkedHashSet, and TreeSet all implement the Set interface. So we have tried to list out the differences and similarities between HashSet, LinkedHa
6 min read