avatarVikram Gupta

Summary

The web content provides an in-depth explanation of the internal workings of Java's HashSet class, emphasizing its reliance on HashMap for storage and operations.

Abstract

The article delves into the inner mechanisms of Java's HashSet, a collection that ensures uniqueness of its elements. It highlights that HashSet is internally implemented using a HashMap, which stores elements as keys with a constant value, PRESENT, as the corresponding value. The initial capacity of a HashSet is 16 with a load factor of 0.75. The add, remove, iterator, size, isEmpty, contains, and clear methods are discussed, illustrating how they delegate operations to the underlying HashMap. The article also suggests that understanding HashMap is crucial for grasping HashSet functionality, and it concludes by directing readers to additional resources on Java collection framework interview questions.

Opinions

  • The author assumes that readers with a grasp of HashMap will find it easier to understand HashSet.
  • It is implied that knowledge of HashSet's internal workings is valuable for job interviews involving Java collections.
  • The article suggests that the use of a dummy PRESENT object in HashSet's implementation is a notable design choice.
  • The author emphasizes the importance of the initial capacity and load factor in the performance of a HashSet.
  • The article conveys that the methods provided by HashSet offer a clear abstraction over the underlying HashMap operations.

JAVA COLLECTION INTERVIEW QUESTIONS

Internal Working of HashSet in Java

Get ready for the HashSet interview question.

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 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 HashSet is 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.
HashSet and HashMap design

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.

You Can Follow Vikram Gupta For Similar Content.

1. Comparing ArrayList and LinkedList in Java

2. Internal Working of HashMap in Java

3. Comparing HashMap and ConcurrentHashMap in Java

4. Complete Guide on LinkedHashMap in Java

5. Internal Working Of TreeMap in Java Collection Framework

6. Internal Working of HashSet in Java

Working Of Hashset
Hashset And Hashmap
Hashset In Java
Java Interview
Java Programming
Recommended from ReadMedium