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.
1 |
public void clear() |
The HashSet object will be empty after this method call. Please visit how to check if HashSet is empty example to know more.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.HashSet; public class JavaHashSetClearExample { public static void main(String[] args) { HashSet<String> hsetColors = new HashSet<String>(); hsetColors.add("Red"); hsetColors.add("Green"); hsetColors.add("Blue"); //this will print false System.out.println("Is HashSet empty? " + hsetColors.isEmpty()); /* * To remove all elements from the HashSet object, use * the clear method. */ hsetColors.clear(); //this will print true now System.out.println("Is HashSet empty? " + hsetColors.isEmpty()); } } |
Output
1 2 |
Is HashSet empty? false Is HashSet empty? true |
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.
1 |
boolean removeAll(Collection<?> c) |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
HashSet<String> hsetColors = new HashSet<String>(); hsetColors.add("Red"); hsetColors.add("Green"); hsetColors.add("Blue"); System.out.println("HashSet contains: " + hsetColors); /* * You can also use the removeAll method to * remove all elements from the HashSet object */ //pass the same HashSet object to clear it hsetColors.removeAll(hsetColors); //this will be empty now System.out.println("HashSet contains: " + hsetColors); |
Output
1 2 |
HashSet contains: [Red, Blue, Green] HashSet contains: [] |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
HashSet<String> hsetColors = new HashSet<String>(); hsetColors.add("Red"); hsetColors.add("Green"); hsetColors.add("Blue"); System.out.println("HashSet contains: " + hsetColors); /* * Assign a new empty HashSet object to the same reference * to make HashSet empty */ hsetColors = new HashSet<String>(); System.out.println("HashSet contains: " + hsetColors); |
Output
1 2 |
HashSet contains: [Red, Blue, Green] HashSet contains: [] |
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