![]() |
VOOZH | about |
The removeAll() method of the ArrayList class in Java is used to remove all elements of an ArrayList that are specified in another collection or within the same list itself.
Example 1: Here, we use the removeAll() method to remove all elements from an ArrayList by passing the same list as an argument.
[]
public boolean removeAll(Collection c)
Example 2: Here, we use removeAll() method to remove specific elements from an ArrayList using another list.
Original list: [1, 2, 3, 4, 5] List after removing specific elements: [4, 5]
Explanation: In the above example, we create two ArrayList of Integers one for storing all the elements and another containing elements to be removed. Then the removeAll() method removes only the specified elements.
Example 3: Here, we use the removeAll() method to demonstrate how passing a null collection as an argument results in a NullPointerException.
Original list: [1, 2, 3, 4, 5] Trying to use removeAll() with a null list. Exception thrown: java.lang.NullPointerException