How to Clear – Remove All Elements from Java LinkedHashSet Example

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.

The LinkedHashSet object becomes empty after this method call.

Output

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.

To make the LinkedHashSet object empty, we will pass the same linked hash set object reference to this method as given below.

Output

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.

Output

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

About the author

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *

Online Shopping