Clear ArrayList with clear() vs. removeAll()

Learn to clear an ArrayList or empty an ArrayList in Java. Clearing a list means removing all elements from the list. It is the same as resetting the list to its initial state when it has no element stored in it.

To clear an arraylist in java, we can use two methods.

Both methods will finally empty the list. But there is a difference in how they perform the empty operation.

1. Clear ArrayList with clear()

The following Java program clears an arraylist using the clear() API.

ArrayList<String> list = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e"));

list.clear();

Assertions.assertEquals(0, list.size());

2. Clear ArrayList with removeAll()

Java program to remove all elements of an arraylist with removeAll() method. The removeAll() method removes all the elements in the current list that are present in the specified collection.

In this example, we are passing the self-reference of the list to the removeAll() method.

ArrayList<String> list = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e"));

list.removeAll(list);

Assertions.assertEquals(0, list.size());

3. Difference between clear() and removeAll()

As I said before, both methods empty a list. But the difference lies in how they make the list clear. Let’s see the code for both methods to understand the actions they perform.

The clear() method is simple. It iterates over the list and assigns null to each index in the list.

public void clear() 
{
    // clear to let GC do its work
    for (int i = 0; i < size; i++)
        elementData[i] = null;
 
    size = 0;
}

In removeAll() method, it first checks if the element is present or not using contains() method. If the element is present then it is removed from the list. This happens for all the elements in the loop.

public boolean removeAll(Collection<?> c) {
    Objects.requireNonNull(c);
    return batchRemove(c, false);
}
 
private boolean batchRemove(Collection<?> c, boolean complement) {
    final Object[] elementData = this.elementData;
    int r = 0, w = 0;
    boolean modified = false;
    try {
        for (; r < size; r++)
            if (c.contains(elementData[r]) == complement)
                elementData[w++] = elementData[r];
    } finally {
        // Preserve behavioral compatibility with AbstractCollection,
        // even if c.contains() throws.
        if (r != size) {
            System.arraycopy(elementData, r,
                             elementData, w,
                             size - r);
            w += size - r;
        }
        if (w != size) {
            // clear to let GC do its work
            for (int i = w; i < size; i++)
                elementData[i] = null;
            modCount += size - w;
            size = w;
            modified = true;
        }
    }
    return modified;
}

4. Conclusion

By going through the sourcecode of both methods, we can safely say that clear() method gives much better performance because of less number of statements it executes.

The removeAll() method lack in performance because of extra calls to contains() method. But, still removeAll() method is useful in cases such as merge two arraylists without duplicate elements.

Happy Learning !!

Read More: ArrayList Java Docs

Sourcecode on Github

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial