Difference between HashSet and HashMap in Java? Answered (original) (raw)
HashSet and HashMap in Java
Hello friends, if you have given Java developer interview then there is good chance that you may have come across questions like Difference between HashSet vs HashMap or HashSet vs TreeSet etc. In this article, we are going to discuss differnece between HashMap and HashSet, two of the popular Collection classes from JDK. The HashSet vs HashMap is a classical Java Collection interview question that focuses on What are differences between HashSet and HashMap in terms of features, usage, and performance. If you are in Java programming even for a year or so, you are likely to be familiar with What is HashSet in Java and What is HashMap in Java, these two are the most popular collection classes.
Despite being hash-based collections HashSet and HashMap are different from each other because the underlying interface is different. HashSet implements Set interface via extending AbstractSet class and HashMap implements Map interface.
This means HashSet is also a Set which means it doesn't maintain order and doesn't allow duplicates, while HashMap is a like a dictionary which can be used to map a key with a value.
Before seeing differences between let's see what is c_ommon between HashSet and HashMap in Java_:
HashMap and HashSet in Java
Here are some of the common stuff between both of them:
1. Common Underlying Data Structure
Both HashMap and HashSet are a hash-based collection in Java. In fact HashSet is implemented using HashMap internally. You may be thinking how come? because HashMAp needs two object, one key and one value but HashSt only need one object?
Well, a default value is used to map with all keys. You can see that when you look into HashSet.java code and you can also read more about that on my post How HashSet works internally in Java where I have covered in depth.
2. Synchronization
Both HashMap and HashSet are **not synchronized **and hence can not be shared between multiple threads. If you need to share then you can use ConcurrentHashMap in Java.
3. Fail-Safe
Both HashMap and HashSet provided constant time performance for basic operations like put() and get(), etc.
Both HashSet and HashMap does not allows null. For example, you cannot add "null" into HashSet as it will throw NullPointerExcpetion, similarly you cannot add null key into HashMap, it will also throw null pointer exception, but HashMap allows null values.
If you need to store null then you can use collection classes like ArrayList or LinkedList or specialized HashSet implementation like LinkedHashSet or ConcurrentHashMap which allows null.
Differences between HashSet and HashMap in Java
After seeing similarities on HashMap and HashSet, now let's see some differences between them :
1. Data Structure
The first and most significant difference between HashMap and HashSet is that HashMap is an implementation of Map interface while HashSet is an implementation of Set interface, which means HashMap is a key value-based data-structure and HashSet guarantees uniqueness by not allowing duplicates.
In reality, HashSet is a wrapper around HashMap in Java, if you look at the code of add(E e) method of HashSet.java you will see the following code :
public boolean add(E e) { return map.put(e, PRESENT)==null; }
where it's putting Object into a map as key and value is a final object PRESENT which is a dummy.
2. Storing function
The second difference between HashMap and HashSet is that we use add() method to put elements into Set but we use the put() method to insert key and value into HashMap in Java.
3. Null Values
HashSet does not allows null objects, but HashMap allows null values. Trying to store null on HashSet will throw NullPointerException.
And, if you are wondering how HashSet works in Java then here is a nice diagram that explains how it is backed up by a HashMap and how it stores elements in form of key-value pair, where values are always the same.
That's all on the difference between HashSet and HashMap in Java. In summary HashSet and HashMap are two different types of Collections one being Set and the other being Map. If you need a set to store unique elements then you should use HashSet in Java. Similarly, if you need a general-purpose hashtable data structure to store keys and values then you should use the HashMap in Java.
Other Java Collection tutorials:
- Difference between HashMap and ConcurrentHashMap in Java
- Difference between TreeMap and TreeSet in Java
- Difference between TreeSet and HashSet in Java
- How to loop or traverse List in Java
- Difference between HashMap and ArrayList in Java
- Difference between HashMap, LinkedHashMap, and TreeMap in Java
- Difference between HashSet, LinkedHashSet, and TreeSet in Java
Thanks for reading this article. If you like this Java Collection interview question and my explanation, then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.