The difference between removeall() and clear() in ArrayList

The difference between removeAll() and clear() in ArrayList

boolean removeAll(Collection<?> c)
Remove all elements of a given collection from the list (optional action).

removeAll(Collection< ?> C) can pass a Collection type parameter, which is intended to clear the current list with all the elements contained in the passed Collection. That is to say, removeAll is actually an operation that first intersects with c and then fills.
And clear() clears all the elements in the current list. Although the names are somewhat similar, they are actually completely different operations.

    1. removeAll is similar to retainAll by taking the intersection of the current list and the incoming Collection. That is to say, the current list does not necessarily contain all the elements of the incoming Collection. The incoming Collection may contain some elements that are not in the current list, but it does not affect the work of removeAll and retainAll. Most collections can use these two methods and pass the Collection as a parameter, not just to List or ArrayList, but to be aware of element type compatibility. RemoveAll removes all elements from the current list that intersect with the passed Collection, rather than just removing the first element from the list, as removeAll does. So, when you want to removeAll the element a from the list, you can use removeAll:
arrayList.removeAll(new ArrayList<Integer>(Arrays.asList(a)));

RetainAll is similar to removeAll in that when an element a is included in the passed Collection, multiple a’s in the list retain both removeAll and retainAll’s behavior depending on the equals() method. You must be aware of the changes

‘s behavior when using
The attached:

boolean retainAll(Collection<?> c)
Keep only those elements of this collection that are also included in the specified collection (optional action). In other words, remove all elements from this collection that are not included in the specified collection.

Read More: