Difference between ArrayList.clear() and ArrayList.removeAll()
Here we are going to have a brief knowledge Difference between ArrayList.clear() and ArrayList.removeAll() in Java.
An ArrayList is a re-sizable array we can also call it a dynamic array. When a new element is to be added it grows its size and we an element is deleted it shrinks it.
ArrayList itself is an array it stores array inside it so whenever we have to retrieve element we can use index same as in array. It provides duplicate and null values.
So the method removeAll() in ArrayList removes a particular element from the list. The particular element is those which are contained in the specified collection.
ArrayList.clear() vs ArrayList.removeAll() with simple example in Java
Suppose we have an ArrayList1 =[8,2,7,4,5] and ArrayList2=[4,5]
so if we want to remove 4 and 5 element from ArrayList1 we write ArrayList1.remove(ArrayList2);
Now ArrayList1 =[8,2,7]
The method clear() in ArrayList is used when we have to remove all the elements from list. Whenever this method is called the list becomes empty.
Suppose we have an ArrayList1 =[8,2,7,4,5]
When the method ArrayList1.clear() is called it clears all the element from the list
so now the list becomes empty, ArrayList1 =
You may also read: