JAVA COLLECTION INTERVIEW QUESTIONS

Internal Working of HashSet in Java

Get ready for the HashSet interview question.

BaseCS101
Javarevisited
Published in
4 min readDec 17, 2022

--

HashSet and HashMap design

While going through the Java interview questions on the collection framework, I have seen a lot of interviewers asking for the internal working mechanism of the hashmap. Okay, you may know that I have already covered that in detail. But in some of the interviews, the interviewer might ask you about HashSet instead of HashMap. If you have gone through my HashMap article then it would become very easy for you to understand the workings of HashSet. Because internally HashSet is backed by a HashMap.

What Is HashSet?

HashSet is one of the data structures in the Java Collections framework.

Let’s see the important points:

  1. It stores unique elements and permits null
  2. It is backed by a HashMap
  3. It doesn’t maintain insertion order
  4. It is not thread-safe

How HashSet Object Is Created and Initialized?

Whenever we create an instance of HashSet it internally creates an instance of HashMap, basically, each HashSet is backed by a HashMap internally. Let’s see the code block to…

--

--

BaseCS101
Javarevisited

-: Empowering Developers to Ace Their Technical Interviews :-