Difference between HashSet and HashMap in Java

HashSet and HashMap in Java
HashSet vs HashMap is a classical Java Collection interview question which 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 most popular collection classes. Despite being hash based collection HashSet and HashMap are different to 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:

Similarities on HashMap and HashSet in Java

Here are some of the common stuff between both of them:

1) Both HashMap and HashSet are a hash based collection in Java.
2) Both HashMap and HashSet are not synchronized and can not be shared between multiple threads.
3) Iterator returned by HashMap's keySet() and HashSet are fail-fast and they throw ConcurrentModificationException if they detect any structural change in Collection.

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

Differences between HashSet and HashMap in Java

Difference between HashSet vs HashMap in JavaAfter seeing similarities on HashMap and HashSet, now let's see some difference between them :

1) 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 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) The second difference between HashMap and HashSet is that we use add() method to put elements into Set but we use put() method to insert key and value into HashMap in Java.

3) HashSet allows only one null key, but HashMap can allow one null key + multiple null values.

That's all on the difference between HashSet and HashMap in Java. In summary HashSet and HashMap are two different type of Collection one being Set and other being Map.

Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures and Algorithms: Deep Dive Using Java

Other Java Collection tutorials:

Thanks for reading this article. If you like this interview question and my explanation, then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.


  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.

  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?

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