Skip to content

Java HashSet Clear – Remove All Elements (Empty) Example

This example shows how to clear HashSet in Java. This example also shows how to remove all elements from the HashSet (empty HashSet) using clear and removeAll methods.

How to clear the HashSet (remove all elements or empty) in Java?

There are several ways using which you can clear or empty HashSet as given below.

1. Using the clear method

The clear method of the Java HashSet class removes all elements from the set object.

The HashSet object will be empty after this method call. Please visit how to check if HashSet is empty example to know more.

Output

2. Using the removeAll method

We can also use the removeAll method inherited from the Set interface to remove all elements from the HashSet object.

The removeAll method removes all elements from this collection that are also present in the argument collection object. We will pass the same HashSet object to this method to empty it as given below.

Output

3. By assigning a new object

We can also assign a new and empty HashSet object to the same reference to make it empty using the HashSet constructor as given below.

Output

What is the preferred way to clear the HashSet (clear vs HashSet constructor vs removeAll method)?

As we have seen, there are multiple ways in which we can empty the HashSet object but which method is best among them? Let’s look at them one by one.

The HashSet clear method calls the clear method of the internal HashMap object. The clear method of the HashMap iterates the map object and assigns null to all keys of the map, thus making them eligible for the garbage collection if they are not referenced from anywhere else. Calling this method does not change the capacity of the HashSet object.

The removeAll method also iterates the HashSet object using an Iterator and removes elements one by one if they are also present in the specified collection object. Calling this method also does not change the capacity of the HashSet object.

When you assign a new HashSet object to the same reference, all the elements plus the HashSet object become eligible for the garbage collection if they are not referenced from anywhere else. Additionally, this approach also needs to create a new HashSet object to assign it to the same reference that is a costly operation in terms of performance. The capacity of the HashSet is reset to the default value (i.e. 16) unless you use the constructor that accepts the custom capacity argument.

The clear and removeAll methods are similar in performance, but the clear method makes code more readable so it should be used instead of the removeAll method.

If you are going add roughly the same number of elements to the HashSet after clearing it, use the clear method instead of creating a new HashSet object using the constructor since it reuses the same object and frequent reallocation is not needed. If not, you can go ahead and assign a new object to the same reference.

This example is a part of the Java HashSet Tutorial with Examples.

Please let me know your views in the comments section below.

References:
Java 8 HashSet

About the author

Leave a Reply

Your email address will not be published.