Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Mar 12, 2024
Contribute to Docs

The .removeIf() method removes all the elements of an ArrayList that satisfy a given predicate. Any error or runtime exception thrown during the iteration or by the predicate is relayed to the caller. If any element is removed, this method returns true. Otherwise, it returns false.


arrayListInstance.removeIf(Predicate<T> filter);
  • arrayListInstance: The name of the ArrayList to be checked.
  • Predicate<T>: A functional interface representing a condition that takes a single argument of type T.
  • filter: The condition that needs to be checked.


In the following example, the .removeIf() method removes all the even elements from an ArrayList called nums:

import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.removeIf(n -> (n % 2 == 0));

Here is the output for the above example:


All contributors

Looking to contribute?

Learn Java on Codecademy