Java ArrayList removeIf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayList

Definition

In Java 8 and later, removeIf(filter) removes every element of an ArrayList for which the filter returns true. The filter is a Predicate<E>, usually written as a lambda such as n -> n < 0.

It is the safe, one-line replacement for removing elements inside a loop. The remaining elements keep their order, and the method returns true if at least one element was removed.

An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe. removeIf has been available since Java 8 as a default method of Collection; ArrayList overrides it with the same behavior.

Syntax

Pass a condition; matching elements are removed.

Java
List<Integer> list = new ArrayList<>(Arrays.asList(5, -2, 8, -1));
boolean any = list.removeIf(n -> n < 0);   // true, list = [5, 8]
list.removeIf(n -> n > 100);               // false, unchanged
CallWhat it does
list.removeIf(x -> cond)Removes all elements where cond is true.
list.removeIf(String::isEmpty)Method reference: removes empty strings.
list.removeIf(Objects::isNull)Removes all null elements.
list.removeIf(x -> true)Removes everything (same result as clear()).

ArrayList's implementation tests the predicate on the elements first and then removes all matches in a single pass, so it avoids the index-shifting mistakes of a hand-written loop.

Library declarations
Java
// java.util.ArrayList<E>
public boolean removeIf(Predicate<? super E> filter)

// Declared in Collection<E> (default method since Java 8)

// java.util.function.Predicate<T>
boolean test(T t)

Parameters

ParameterDescription
filter (Predicate<? super E>)Returns true for elements to remove. Must not be null.

Return Value

Return ValueDescription
booleantrue if any element was removed; otherwise false.

Compare with Similar Methods

Other ways to remove several elements:

MethodHow it differs
remove(o)Removes one element by value or index.
removeAll(c)Removes elements listed in another collection.
retainAll(c)Keeps elements listed in another collection.
iterator()it.remove() inside a loop, for logic that does not fit one lambda.

Java Examples

Example 1 — Remove Negative Readings

Drop every negative sensor reading in one call.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListRemoveIfDemo {
    public static void main(String[] args) {
        List<Integer> readings = new ArrayList<>(Arrays.asList(12, -3, 7, -8, 0, 5));

        boolean removed = readings.removeIf(r -> r < 0);
        System.out.println(removed + " " + readings);
    }
}

How It Works

  1. A sensor list. Negative numbers here are errors that should be dropped.
  2. One condition. The lambda r -> r < 0 returns true for -3 and -8.
  3. Both removed in one call. The rest keep their order, and 0 stays because 0 < 0 is false.
  4. Report. removeIf returns true because the list changed.

Example 2 — Clean Up nulls and Blank Strings

First remove null entries, then remove strings that are empty after trimming.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class ArrayListRemoveIfClean {
    public static void main(String[] args) {
        List<String> inputs = new ArrayList<>(Arrays.asList("ok", "", null, "  ", "done", null));

        inputs.removeIf(Objects::isNull);
        System.out.println("no nulls: " + inputs);

        inputs.removeIf(s -> s.trim().isEmpty());
        System.out.println("no blanks: " + inputs + " size=" + inputs.size());
    }
}

How It Works

  1. nulls first. Objects::isNull matches the two null entries, and removing them first means the next lambda can safely call methods on every element.
  2. Hard-to-see blanks. The empty string and the two-space string print as gaps between the commas.
  3. Trim and test. s.trim().isEmpty() is true for both "" and " ", so both are removed.
  4. Order matters. Running the trim lambda while the null entries were still present would throw NullPointerException from s.trim().

Example 3 — removeIf() vs Removing in a for-each Loop

The tempting for-each loop fails halfway; removeIf finishes the job.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.List;

public class ArrayListRemoveIfVsLoop {
    public static void main(String[] args) {
        List<String> words = new ArrayList<>(Arrays.asList("ant", "bee", "cat", "bat"));

        try {
            for (String w : words) {
                if (w.startsWith("b")) {
                    words.remove(w);
                }
            }
        } catch (ConcurrentModificationException e) {
            System.out.println("loop failed: " + e.getClass().getSimpleName());
        }
        System.out.println("after loop: " + words);

        words.removeIf(w -> w.startsWith("b"));
        System.out.println("after removeIf: " + words);
    }
}

How It Works

  1. The tempting loop. Calling words.remove(w) inside a for-each loop changes the list behind the hidden iterator's back.
  2. Fail-fast. After "bee" is removed, the iterator's next call notices the change and throws ConcurrentModificationException.
  3. Half finished. "bee" is gone, but "bat" was never reached, so the list is left in a partial state.
  4. One safe call. removeIf handles the walk and the removal together and removes "bat" without any exception.

Frequently Asked Questions

It removes every element for which the given predicate returns true, and keeps the rest in order.
Java 8, as a default method of the Collection interface.
Removing inside a for-each loop throws ConcurrentModificationException, and a forward index loop skips elements. removeIf avoids both problems.
true if at least one element was removed, otherwise false.
No. The predicate receives each element, not its index. To remove a range of indexes, use list.subList(from, to).clear().

Next: removeLast()

Remove and return the last element (Java 21+).

removeLast() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful