JAVA COLLECTION INTERVIEW QUESTIONS
Internal Working of HashSet in Java
Get ready for the HashSet interview question.

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:
- It stores
uniqueelements and permits null - It is backed by a
HashMap - It doesn’t maintain insertion order
- 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 understand this:
HashSet Object Creation:
Set<String> colorSet = new HashSet<String>();HashSet Constructor:
private transient HashMap<E,Object> map;
// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();
/**
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
* default initial capacity (16) and load factor (0.75).
*/
public HashSet() {
map = new HashMap<>();
}Every HashSet object has a map field of type HashMap. This reference variable gets initialized when the HashSet() constructor is called at the time of the creation of the HashSet object.
Note that HashMap stores the entries in key-value pairs but HashSet is a set of unique elements. Hence the HashSet entries are made key-value pairs using a dummy Object called PRESENT.
Initial capacity of
HashSetis 16 and the load factor of 0.75.
How the Elements Are Added to HashSet?
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}The add method adds the specified element to this set if it is not already present in the backup map. If this set already contains the element, the method call leaves the set unchanged, and the add method returns false. Otherwise, it returns true when added successfully.
How To Remove an Element From HashSet?
public boolean remove(Object o) {
return map.remove(o)==PRESENT;
}The call to remove(Object o) removes the specified element from the set if it is present and returns true otherwise returns false.
Now Let’s See a Few Other Important Methods of the HashSet Class:
iterator()returns an iterator over the elements in this set and the elements returned are in no particular order.
public Iterator<E> iterator() {
return map.keySet().iterator();
}size()returns the number of elements present in this set.
public int size() {
return map.size();
}isEmpty()returns true if this set contains no elements.
public boolean isEmpty() {
return map.isEmpty();
}contains(Object o)returns true if this set contains the specified element.
public boolean contains(Object o) {
return map.containsKey(o);
}clear()removes all the elements of this set and the set will be empty after this method call.
public void clear() {
map.clear();
}- Refer to the image below to get a complete idea of how the set and map are constructed.

Now let’s wrap up this article. I will be writing more on collections interview questions in upcoming articles. Till then you can refer to the previously written interview questions on ArrayList, LinkedList, HashMap and ConcurrentHashMap.
That’s all for this article. Hope you enjoyed it.




