Disclosure: This article may contain affiliate links. When you purchase, we may earn an affiliate commission.

Difference between HashSet and HashMap in Java? Answer

HashSet and HashMap in Java
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 AbstractSetclass and HashMap implements Map interface. Before seeing differences between let's see what is common 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.


2. Synchronization
Both HashMap and HashSet are not synchronized and can not be shared between multiple threads.


3. Fail-Safe
Iterator returned by HashMap's keySet() and HashSet are fail-fast and they throw ConcurrentModificationException if they detect any structural change in Collection.

4. Performance
Both HashMap and HashSet provided constant time performance for basic operations like put(), get(), etc.


5.Nulls
 Both HashSet and HashMap allows null values.



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 allows only one null key, but HashMap can allow one null key + multiple null values.

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. 


Difference between HashSet and HashMap in Java? Answer


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:

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.

3 comments:

  1. Does difference between HashMap vs HashSet is similar to difference between Map vs Set ? I don't see any other difference, it looks all the points mentioned about is actually true for Map vs Set as well.

    ReplyDelete
  2. Point 3 in the difference says,HashSet allows only one null key.Basically HashSet does not allow null and it throw null pointer exception.Also Hashset does not have a key concept since it's implementation of Set interface.Can you please confirm this statement?

    ReplyDelete
    Replies
    1. HashSet allows null values however if you insert more than one nulls it would still return only one null value.

      Delete

Feel free to comment, ask questions if you have any doubt.