Java LinkedList removeIf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

Definition

In Java, removeIf(filter) goes through the LinkedList and removes every element for which the Predicate returns true. A predicate is a small test, usually written as a lambda such as n -> n < 0.

It replaces the old pattern of looping with an iterator and calling remove() yourself. It works in one pass and is safe, since the removal is done by the method and not by a loop over the same list.

An instance method of java.util.LinkedList (since Java 8): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by Collection, and LinkedList implements it. removeIf() was added in Java 8.

Syntax

Pass a lambda or a method reference that returns a boolean.

Java
LinkedList<Integer> nums = new LinkedList<>(Arrays.asList(5, -2, 8, -1));
boolean changed = nums.removeIf(n -> n < 0);   // true
// nums is now [5, 8]
CallWhat it does
list.removeIf(x -> test)Removes every element where the test is true.
list.removeIf(Objects::isNull)Removes all null elements.
list.removeAll(c)Removes elements by membership in a collection.
list.iterator().remove()The older way: remove while iterating by hand.

The test decides what is removed, not what is kept. Read x -> x < 0 as "remove x if x is less than 0".

Library declarations
Java
// java.util.LinkedList (default method from java.util.Collection)
public boolean removeIf(Predicate<? super E> filter)

Parameters

ParameterDescription
filter (Predicate<? super E>)A test that returns true for the elements to remove. It must not be null.

Return Value

Return ValueDescription
booleantrue if at least one element was removed.

Compare with Similar Methods

Ways to remove several elements:

MethodHow it differs
removeAll(c)Removes elements that appear in another collection.
iterator()Lets you call remove() while walking the list, the pre-Java 8 way.
retainAll(c)Keeps elements that appear in another collection.
ArrayList.removeIf()The same default method on an array-backed list.

Java Examples

Example 1 — Remove Negative Numbers with a Lambda

Remove all negative numbers from a list in one call.

Java
import java.util.Arrays;
import java.util.LinkedList;

public class LinkedListRemoveIfDemo {
    public static void main(String[] args) {
        LinkedList<Integer> temps = new LinkedList<>(Arrays.asList(12, -3, 7, -8, 0, 15));
        System.out.println("before: " + temps);

        boolean changed = temps.removeIf(t -> t < 0);

        System.out.println("changed: " + changed);
        System.out.println("after:  " + temps);
    }
}

How It Works

  1. A mixed list. Two of the six temperatures are below zero.
  2. The lambda is the test. t -> t < 0 is true for -3 and -8, which are the ones to remove.
  3. Zero stays. 0 is not less than 0, so the test is false for it.
  4. The result is true. At least one element was removed, and the remaining four keep their order.

Example 2 — Remove Blank and Short Words

Remove empty strings and words shorter than three letters from a list of words.

Java
import java.util.Arrays;
import java.util.LinkedList;

public class LinkedListRemoveIfText {
    public static void main(String[] args) {
        LinkedList<String> words = new LinkedList<>(
            Arrays.asList("code", "", "to", "fun", "a", "learn"));

        words.removeIf(w -> w.isEmpty());
        System.out.println("no blanks: " + words);

        words.removeIf(w -> w.length() < 3);
        System.out.println("long enough: " + words);

        System.out.println("nothing left to remove: " + words.removeIf(w -> w.length() > 10));
    }
}

How It Works

  1. Six strings. One is empty, and two others have fewer than three letters.
  2. First pass. The empty string is the only one for which isEmpty() is true.
  3. Second pass. "to" and "a" are shorter than three letters, so they are removed too.
  4. A pass can find nothing. No word is longer than 10 letters, so the last call returns false.

Example 3 — Remove null Elements with a Method Reference

Use Objects::isNull to clean up a list that contains nulls.

Java
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Objects;

public class LinkedListRemoveIfNulls {
    public static void main(String[] args) {
        LinkedList<String> data = new LinkedList<>(
            Arrays.asList("a", null, "b", null, null, "c"));
        System.out.println("before: " + data + " size " + data.size());

        data.removeIf(Objects::isNull);

        System.out.println("after:  " + data + " size " + data.size());

        try {
            data.removeIf(null);
        } catch (NullPointerException e) {
            System.out.println("removeIf(null) -> NullPointerException");
        }
    }
}

How It Works

  1. Three nulls in six elements. They sit at indexes 1, 3 and 4.
  2. A method reference as the test. Objects::isNull is true for exactly the null elements.
  3. All nulls are gone. The list is a, b, c, and the size drops from 6 to 3.
  4. A null filter is rejected. Passing null as the predicate throws NullPointerException.

Frequently Asked Questions

It removes every element for which the given Predicate returns true, and returns true if at least one element was removed.
Java 8, together with lambdas and the Predicate interface.
Call list.removeIf(Objects::isNull) after importing java.util.Objects.
removeIf uses a rule (a Predicate), and removeAll uses another collection of values.
The predicate marks the elements to remove, not the ones to keep. Check that your condition is true for the elements you want to delete.

Next: removeLast()

Remove and return the last element, or throw if empty.

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.

13 people found this page helpful