How to empty an ArrayList in Java? clear() vs removeAll() method Example Tutorial (original) (raw)

Many times we want to reset an ArrayList for the reusing purpose, by resetting we mean clearing it or removing all elements. There are two ways to reset an ArrayList in Java, by using the clear() method or calling removeAll(). If your ArrayList is small enough like it contains only 10 or 100 elements then you can use any of these two methods without worrying too much, but, if you have a huge list of lots of objects like an ArrayList containing 10M entries, then the choice of clear() vs removeAll() can make a huge difference in the performance of your Java application.

Sometimes it's even better to create a new ArrayList instead of resetting the old one, especially if resetting takes a long time, but this also has a caveat, you need to make sure that the old ArrayList is eligible for garbage collection, otherwise, there is a huge risk of java.lang.OutOfMemoryError: Java Heap Space.

Coming back to the clear() vs removeAll() method, you should always use clear(), because it gives you O(n) performance, while removeAll(Collection c) is worse, it gives O(n^2) performance, that's why you see a huge difference in time taken by clearing a large ArrayList by these two methods.

Things will be obvious, when you will run our example program and see the code of clear() and removeAll() method from JDK API.

By the way, if you are in doubt, use the clear() method, and if not then always prefer clear over removeAll in Java.

Clear() vs RemoveAll(Collection c) Example in Java

In order to compare the performance of both these methods, it's very important to see their code. You can check the source code of the clear() method in java.util.ArrayList class, for convenience I have included it here.

This code is from JDK version 1.7.0_40. By the way, If you want to learn more about performance measurement and tuning then I strongly suggest joining these best Java performance online courses for beginners and experienced developers.

It covers both processes and tools you need to monitor and tune the performance of Java applications.

/** * Removes all of the elements from this list. The list will * be empty after this call returns. */ public void clear() { modCount++; // clear to let GC do its work for (int i = 0; i < size; i++) elementData[i] = null; size = 0; }

You can see that this method loop over ArrayList and assign null to every element to make them eligible for garbage collection, of course, if there is no external reference.

How to empty an ArrayList in Java? Clear vs RemoveAll Example Tutorial

Similarly, you can check the source code of java.util.AbstractCollection class to look at how the removeAll(Collection c) method works, here is a snippet:

public boolean removeAll(Collection c) { boolean modified = false; Iterator it = iterator(); while (it.hasNext()) { if (c.contains(it.next())) { it.remove(); modified = true; } } return modified; }

This implementation iterate over the collection, checking each element returned by the iterator, in turn, to see if it's contained in the specified collection. If it's present then it is removed from this collection by using Iterator's remove method.

Because of using contains() method, removeAll() performance goes into the range of O(n^2), which is an absolutely NO, especially if you are trying to reset a large ArrayList. Now let's see their performance in action to reset an ArrayList of just 100K entries.

If you are interested more in Java Performance measurement and tuning then I also suggest you take a look at Java Performance The Definitive Guide By Scott Oaks, one of the best books on Java profiling.

clear vs removeAll two ways to remove all elements form ArrayList

Removing all elements from ArrayList with 100K Objects

I have initially tried to run this example with 10M elements but after waiting for more than half an hour to let removeAll() finish, I decided to reduce the number of objects to 100K, even then the difference between the time taken by clear() vs removeAll() is quite significant. The removeAll(Collection c) is taking 10000 times more time than clear to reset.

Actually, the purpose of clear() and removeAll(Collection c) are different in API, the clear() method is meant to reset a Collection by removing all elements, while removeAll(Collection c) only removes elements that are present in the supplied collection. This method is not designed to remove all elements from a Collection.

So, if your intention is to delete all elements from a Collection, then use clear(), while if you want to remove only some elements, which are present in another Collection, e.g. list of closed orders, then use the removeAll() method.

import java.util.ArrayList;

/**

}

Output: Time taken by clear to empty ArrayList of 100000 elements (ns): 889619 Time taken by removeAll to reset ArrayList of 100000 elements (ns): 36633112126

Make sure you provide sufficient memory to run this program because it uses two ArrayList to store Integers, especially if you want to compare the performance of clear() and removeAll() for List with 1M elements.

You also need Java 7 to run this program because I am using underscore with the numeric literal feature. If you don't have JDK 7 then just remove underscores from SIZE constants, those are just for improving readability.

That's all about how to reset an ArrayList in Java. We have not only learned two ways to remove all elements from ArrayList but also learned about the difference between the clear vs removeAll method. We have seen that removeAll performs poorly when the list is large and that's why you should always prefer clear() over removeAll() in Java.

By the way, if clearing ArrayList is taking significant time, consider using the new ArrayList, as Java is pretty fast in creating objects.

Other Java ArrayList tutorials you may like

If you are interested to learn more about the ArrayList class, one of the most useful classes from the Java collection framework, then you might like the following articles as well:

Thanks for reading this article so far. If you like this tutorial and my example of ArrayList.clear() and ArrayList.removeAll() method to empty an ArrayList in Java then please share with your friends and colleagues. If you have any questions, please drop a note.

P. S. - If you are keen to level up your Java collections skills and need resources then I highly recommend you to check out these best Java collections and Stream Courses to start with. It contains some high-quality online courses to master both Collections and Stream API in Java.

Also, what is your favorite way to empty an ArrayList in Java? clear(), removeAll() or anything else? Let me know in comments.