This example shows how to clear or remove all elements from the LinkedHashSet in Java. This example also shows how to clear LinkedHashSet object using the clear and removeAll methods.
How to clear or remove all elements from the LinkedHashSet in Java?
There are several ways using which we can clear the linked hash set object as given below.
1. Using the clear method
The clear
method of the LinkedHashSet class removes all elements from the linked hash set object.
1 |
public void clear() |
The LinkedHashSet object becomes empty after this method call.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.LinkedHashSet; public class LinkedHashSetClearExample { public static void main(String[] args) { LinkedHashSet<String> lhSetColors = new LinkedHashSet<String>(); lhSetColors.add("red"); lhSetColors.add("green"); lhSetColors.add("blue"); /* * To remove all elements from the LinkedHashSet object, * use the clear method */ lhSetColors.clear(); //this will print true System.out.println( lhSetColors.isEmpty() ); System.out.println("LinkedHashSet contains: " + lhSetColors); } } |
Output
1 2 |
true LinkedHashSet contains: [] |
Please also visit how to check if LinkedHashSet is empty example to know more.
2. Using the removeAll method
The removeAll
method of the LinkedHashSet class removes all elements from this set that are also present in the specified collection object.
1 |
boolean removeAll(Collection<?> collection) |
To make the LinkedHashSet object empty, we will pass the same linked hash set object reference to this method as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
LinkedHashSet<String> lhSetColors = new LinkedHashSet<String>(); lhSetColors.add("red"); lhSetColors.add("green"); lhSetColors.add("blue"); /* * You can also use the removeAll method. * Pass the same set object reference to this * method. */ lhSetColors.removeAll(lhSetColors); //this will print true System.out.println( lhSetColors.isEmpty() ); System.out.println("LinkedHashSet contains: " + lhSetColors); |
Output
1 2 |
true LinkedHashSet contains: [] |
3. By assigning a new object
We can also assign a new object to the same LinkedHashSet reference to indirectly make it empty as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
LinkedHashSet<String> lhSetColors = new LinkedHashSet<String>(); lhSetColors.add("red"); lhSetColors.add("green"); lhSetColors.add("blue"); /* * Assign a new empty LinkedHashSet object to * the same reference */ lhSetColors = new LinkedHashSet<String>(); //this will print true System.out.println( lhSetColors.isEmpty() ); System.out.println("LinkedHashSet contains: " + lhSetColors); |
Output
1 2 |
true LinkedHashSet contains: [] |
Which method is preferred to clear the LinkedHashSet?
The clear
method iterates the internal HashMap object and assigns a null value to them. The removeAll
method also iterates the internal map object and removes the elements using the iterator‘s remove
method if they exist in the specified collection. None of these methods changes the capacity of the internal map object.
All the elements of the LinkedHashSet object are eligible for garbage collection (if they are not referenced from anywhere else) after these method calls. However, the clear
method should perform faster than the removeAll
method because it does not need an additional check in the specified collection object.
When you assign a new object to the same reference, all the elements of the LinkedHashSet object plus the original LinkedHashSet object are marked as eligible for the garbage collection. This approach also needs creating a new object that is a costly operation. Plus, since it is a new LinkedHashSet object altogether, the capacity of the internal map resets to default.
If you are going to add a similar number of elements after clearing the LinkedHashSet, using the clear
method is the preferred way to avoid frequent allocation of the capacity. If not, you may assign a new object to the same reference.
This example is a part of the Java LinkedHashSet Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashSet