Difference between HashMap and HashSet
HashSet is 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 map 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 order of the objects but HashMap will not.HashMap allows null values and null keys.
Both HashSet and HashMap are not synchronized.
Difference between HashMap and HashSet

1) Implementation: HashMap implements Map interface and HashSet implements Set interface.
2) Duplicates: HashSet does’t allow duplicate values. HashMap store key, value pairs and it does not allow duplicate keys. If key is duplicate then old key is replaced with new value.
3) 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) .
4) 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.
5) Storing or Adding mechanism : HashMap internally uses hashing to store or add objects, HashSet internally uses HashMap object to store or add the objects.
6) Faster:HashSet is slower then HashMap,
7) Insertion HashMap use put() method for storing data, While in HashSet use add() method for add or storing data.
8) 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}
Notice in my example above that in the HashMap there must not be duplicate keys, but it may have duplicate values.
In the HashSet, there must be no duplicate elements
Example of HashSet and HashMap in Java
HashSet
// Java program to demonstrate working of HashSet import java.util.HashSet; public class HashSetExample { 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]
HashMap
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(12, "geeks"); System.out.println("\nAfter inserting duplicate key :\n\n" + hm); } } |
HashMap object output :
{2=practice, 7=contribute, 12=geeks}
After inserting duplicate key :
{2=practice, 7=contribute, 12=geeks}
Recommended Posts:
- Difference between ArrayList and HashSet in Java
- Difference between HashMap and ConcurrentHashMap
- Difference and similarities between HashSet, LinkedHashSet and TreeSet in Java
- HashSet in Java
- HashSet contains() Method in Java
- Initializing HashSet in Java
- Traverse through a HashSet in Java
- HashSet vs TreeSet in Java
- HashSet add() Method in Java
- How to sort HashSet in Java
- Convert HashSet to TreeSet in Java
- HashSet remove() Method in Java
- HashSet retainAll() method in Java with Example
- Internal working of Set/HashSet in Java
- HashSet clear() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



