Java Reference Library →

ArrayList removeIf() method - Syntax and Examples

ArrayList removeIf() method - Syntax and Examples

Learn how to use the Java ArrayList's `removeIf()` method to remove elements based on a predicate. Includes syntax, parameters, return value, and practical examples.

This reference explains ArrayList removeIf() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.

  1. Use this reference

    Identify the documented behavior, read a complete example and check its inputs before applying it.

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.

Using this reference

Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.

Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide.

Reference checks are optional and do not count toward course progress.

    Reference library →