Java ArrayList removeIf() method - Syntax and Examples

Introduction

The `removeIf()` method in Java's `ArrayList` is a powerful tool for removing elements that meet a specific condition. Instead of manually iterating through the list and removing elements one by one (which can be tricky due to index shifting), `removeIf()` provides a concise and efficient way to achieve this using a predicate.

Syntax

Here's the syntax for the `removeIf()` method:
public boolean removeIf(Predicate<? super E> filter)

Parameters

Let's break down what that means with a table.
Parameter Description
filter A predicate (a functional interface) that tests each element of the list. It takes an element as input and returns a boolean value: true if the element should be removed, and false otherwise.

Return Value

The `removeIf()` method returns a `boolean` value. It returns true if at least one element was removed from the list; otherwise, it returns false.

Examples

Removing Even Numbers

This example demonstrates how to remove all even numbers from an ArrayList.

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class RemoveIfExample {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(4);
        numbers.add(5);
        numbers.add(6);

        System.out.println("Original list: " + numbers);

        boolean removed = numbers.removeIf(n -> n % 2 == 0); //Predicate to check if a number is even

        System.out.println("List after removing even numbers: " + numbers);
        System.out.println("Elements were removed? " + removed);
    }
}
Original list: [1, 2, 3, 4, 5, 6]
List after removing even numbers: [1, 3, 5]
Elements were removed? true

Explanation:

  • We create an `ArrayList` of integers.
  • The `removeIf()` method is called with a lambda expression (n -> n % 2 == 0) as the predicate. This lambda checks if a number is even by using the modulo operator (`%`). If the remainder when divided by 2 is 0, it's an even number and will be removed.
  • The `removed` variable stores whether any elements were actually deleted.

Removing Strings Shorter Than Length 5

This example shows how to remove strings from an ArrayList that have a length less than 5.

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class RemoveIfStringExample {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");
        names.add("Dave");
        names.add("Eve");

        System.out.println("Original list: " + names);

        boolean removed = names.removeIf(s -> s.length() < 5); //Predicate to check string length

        System.out.println("List after removing strings shorter than 5 characters: " + names);
        System.out.println("Elements were removed? " + removed);
    }
}
Original list: [Alice, Bob, Charlie, Dave, Eve]
List after removing strings shorter than 5 characters: [Alice, Charlie, Dave]
Elements were removed? true

Explanation:

  • We create an `ArrayList` of strings.
  • The `removeIf()` method is called with a lambda expression (s -> s.length() < 5) as the predicate. This lambda checks if the length of a string is less than 5. If it is, the string is removed.

No Elements Removed

This example shows what happens when no elements satisfy the removal condition.

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class RemoveIfNoMatchExample {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(3);
        numbers.add(5);

        System.out.println("Original list: " + numbers);

        boolean removed = numbers.removeIf(n -> n % 2 == 0); //Predicate to check if a number is even, but none are

        System.out.println("List after attempting to remove even numbers: " + numbers);
        System.out.println("Elements were removed? " + removed);
    }
}
Original list: [1, 3, 5]
List after attempting to remove even numbers: [1, 3, 5]
Elements were removed? false

Explanation:

  • We create an `ArrayList` of integers.
  • The `removeIf()` method is called with a lambda expression (n -> n % 2 == 0) as the predicate. However, none of the numbers in the list are even.
  • Because no elements matched the condition, the list remains unchanged and the `removed` variable is set to false.
Mallikarjuna Mallisetty, author
About the author

Mallikarjuna Mallisetty

General programming

Mallikarjuna shares practical programming tutorials and foundational concepts designed to help developers learn by building and experimenting.

View LinkedIn profile ↗