Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:


1. Overview

In this quick tutorial, we'll learn about two Collection methods that might seem to do the same thing, but aren't: clear() and removeAll().

We'll first see the method definitions and then use them in short examples.

2. Collection.clear()

We'll first dive into the Collection.clear() method. Let's check the Javadoc of the method. According to it, the purpose of clear() is to remove every single element from the list.

So, basically, calling clear() on any list will result in the list becoming empty.

3. Collection.removeAll()

We'll now have a look at the Javadoc of Collection.removeAll(). We can see that the method takes a Collection as an argument. And its purpose is to remove all common elements between the list and the collection.

So, when calling it on a collection, it will remove all elements from the passed argument that are also in the collection on which we call removeAll().

4. Examples

Let's now look at some code to see those methods in action. We'll first create a test class with the name ClearVsRemoveAllUnitTest.

After that, we'll create a first test for Collection.clear().

We'll initialize a collection of Integers with a few numbers and call clear() on it so that no element remains in the list:

void whenClear_thenListBecomesEmpty() {
    Collection<Integer> collection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));



As we can see the collection is empty after clear() being called.

Let's create a second test with two collections, one with numbers from 1 to 5 and the other with numbers from 3 to 7. After that, we'll call removeAll() on the first collection with the second one as a parameter.

We'll expect only the numbers 1 and 2 to remain in the first collection (while the second one is unchanged):

void whenRemoveAll_thenFirstListMissElementsFromSecondList() {
    Collection<Integer> firstCollection = new ArrayList<>(
      Arrays.asList(1, 2, 3, 4, 5));
    Collection<Integer> secondCollection = new ArrayList<>(
      Arrays.asList(3, 4, 5, 6, 7));


      Arrays.asList(1, 2), 
      Arrays.asList(3, 4, 5, 6, 7), 

And our expectations are met. Only the numbers 1 and 2 are remaining in the first collection and the second one hasn't been changed.

5. Conclusion

In this article, we've seen the purposes of Collection.clear() and Collection.removeAll().

Despite what we might think at first, they aren't doing the same thing. clear() deletes every element from the collection and removeAll() one only removes the elements matching those from another Collection.

And, as always, the code can be found over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:


Generic footer banner
Comments are closed on this article!