VOOZH about

URL: https://www.geeksforgeeks.org/java/arraylist-removeall-method-in-java-with-examples/

⇱ ArrayList removeAll() Method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

ArrayList removeAll() Method in Java with Examples

Last Updated : 6 Aug, 2025

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.


Output
[]

Syntax of ArrayList removeAll() Method

public boolean removeAll(Collection c)

  • Parameters: This method takes "Collection c" as a parameter containing elements to be removed from this list.
  • Returns Value: This method returns true if the list was modified as a result of the operation, otherwise false.
  • Exception: This method throws NullPointerException if the list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null.

Example 2: Here, we use removeAll() method to remove specific elements from an ArrayList using another list.


Output
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.


Output
Original list: [1, 2, 3, 4, 5]

Trying to use removeAll() with a null list.
Exception thrown: java.lang.NullPointerException
Comment