How HashSet works in java

In this post, we will see about Hashset in java

Java HashSet:

This is one of the frequently asked question in core java interview so in this post, we will see how HashSet works in java.We have already seen How hashMap works in java and also difference between HashMap and HashSet.

Lets first see introduction of Hashset then we will go through internals of it.

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️


HashSet implements Set interface which does not allow duplicate value.It is not synchronized and is not thread safe.
Definition of duplicate can be quite tricky sometimes.Lets consider two cases here.

  1. In case of primitive types(such as interger, String)
  2. In case of custom defined objects.
In case of primitive types:
In case of primitives type, it is very straight forward.Lets see with help of example:
Lets create a java program:
When you run above program, you will get following output:
So we tried to add String “Arpit” twice, but as HashSet does not allow duplicate value, it will add “Arpit” once in HashSet

In case of Custom Objects:
For understanding how HashSet will work in case of custom objects, you need to understand hashcode and equals method in java.Lets create a class called Country and implement only equals method in it.

create main class:

When you run above program, you will get following output:

Now you must be wondering even through two objects are equal why HashSet contains two values instead of one.This is because First HashSet calculates hashcode for that key object, if hashcodes are same then only it checks for equals method and because hashcode for above two country objects uses default hashcode method,Both will have different memory address hence different hashcode.
Now lets add hashcode method in above Country class

Run above main program again, you will get following output:

So now we have good understanding of HashSet, lets see its internal representation:

Internal working of HashSet:

When you add any duplicate element to HashSet, add() method returns false and do not add duplicate element to HashSet.
How add method return false? For this, we need to see HashSet’s add method in JavaAPI
So from above code, It is clear that HashSet uses HashMap for checking duplicate elements.As we know that in HashMap , key should be unique. So HashSet uses this concept, When element is added to HashSet, it is added to internal HashMap as Key.This HashMap required some value so a dummy Object(PRESENT) is used as value in this HashMap.
PRESENT is dummy value which is used value for internal map.
Lets see add method:
So here there will be two cases

  • map.put(e,PRESENT) will return null, if element not present in that map. So map.put(e, PRESENT) == null will return true ,hence add method will return true and element will be added in HashSet.
  • map.put(e,PRESENT) will return old value ,if element is already present in that map. So  map.put(e, PRESENT) == null will return false, hence add method will return false and element will not be added in HashSet.

Please go through  core java interview questions for beginners for more interview questions.


You may also like:

Related Posts

  • 15 April

    How to Deep Copy Arraylist in Java

    Learn about how to create deep copy of ArrayList in different ways.

  • 02 October

    Initialize ArrayList with values in Java

    In this article, we will learn to initialize ArrayList with values in Java. ArrayList is an implementation class of List interface in Java. It is used to store elements. It is based on a dynamic array concept that grows accordingly. We can Initialize ArrayList with values in several ways. Let’s see some of them with […]

  • 17 March

    PriorityQueue in Java 8

    In this post, we will see about Java 8 PriorityQueue. When the objects are supposed to be processed on the basis of their priority, in that scenario we use PriorityQueue. It’s a special type of queue (also, unbound queues) where the elements can be ordered either as per their natural ordering or based on a […]

  • 06 October

    2d Arraylist java example

    In this post, we will see how to create 2d Arraylist in java. Best way to create 2d Arraylist is to create list of list in java. [crayon-607a51a786468641464819/] Let’s create a program to implement 2d Arraylist java. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for tech events? […]

  • 04 May

    How HashMap works in java

    Most common interview questions are <code>How HashMap works in java</code>, “How get and put method of HashMap work internally”. Here I am trying to explain internal functionality with an easy example. [crayon-607a51a7866ef600902052-i/]  is one of the most used Collections in java.Rather than going through theory, we will start with example first, so that you will […]

  • 21 October

    How to Sort HashSet in Java

    In this post, we will see how to sort HashSet in java. HashSet is a collection which does not store elements in any order. You might come across a situation where you need to sort HashSet. There can be many ways to sort HashSet, we will see two methods here. 💻 Awesome Tech Resources: Looking […]


Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.