Java ArrayDeque removeIf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, removeIf(filter) removes every element of an ArrayDeque for which the Predicate filter returns true. The remaining elements keep their order.

It returns true if at least one element was removed. The filter is usually a lambda such as n -> n % 2 == 0. It replaces the older pattern of looping with an iterator() and calling it.remove().

An instance method of java.util.ArrayDeque (Java 1.6+): a resizable-array deque with no capacity limit, no null elements, and no thread safety. removeIf has been available since Java 8 (as a default method of Collection); newer JDKs override it in ArrayDeque with the same behavior.

Syntax

Pass a lambda (or a Predicate variable) that returns true for elements to remove.

Java
Deque<Integer> deque = new ArrayDeque<>(Arrays.asList(1, 2, 3, 4));
boolean changed = deque.removeIf(n -> n % 2 == 0);   // true, [1, 3]
CallWhat it does
deque.removeIf(x -> test)Removes every element where test is true.
deque.removeIf(predicate)Same, using a Predicate variable.
deque.removeIf(x -> false)Removes nothing, returns false.
deque.removeIf(null)Throws NullPointerException.

The filter should only look at the element; do not change the deque from inside it. Variables from the surrounding method that the lambda uses must be effectively final.

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

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

Parameters

ParameterDescription
filterRequired. A Predicate that returns true for each element to remove. Must not be null.

Return Value

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

Compare with Similar Methods

Choosing a removal method:

MethodHow it differs
removeAll(c)Removes by membership in another collection instead of a condition.
retainAll(c)Keeps by membership; removes everything else.
removeFirstOccurrence(o)Removes just one element equal to a value.
Iterator.remove()The pre-Java 8 way: loop with an iterator and remove as you go.

Java Examples

Example 1 — Remove All Even Numbers

One lambda removes every even number.

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class ArrayDequeRemoveIfDemo {
    public static void main(String[] args) {
        Deque<Integer> nums = new ArrayDeque<>(Arrays.asList(1, 2, 3, 4, 5, 6));

        boolean changed = nums.removeIf(n -> n % 2 == 0);

        System.out.println(changed);
        System.out.println(nums);
    }
}

How It Works

  1. The filter. n -> n % 2 == 0 is true for 2, 4, and 6.
  2. All matches removed. Those three elements are removed in one call.
  3. Survivors in order. The odd numbers stay in their original order: [1, 3, 5].
  4. Result. Something was removed, so changed is true.

Example 2 — No Match, Then a Match

A filter that matches nothing returns false; a second filter removes words starting with s.

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class ArrayDequeRemoveIfWords {
    public static void main(String[] args) {
        Deque<String> words = new ArrayDeque<>(Arrays.asList("sun", "moon", "star"));

        System.out.println(words.removeIf(w -> w.length() > 10));
        System.out.println(words.removeIf(w -> w.startsWith("s")));
        System.out.println(words);
    }
}

How It Works

  1. No long words. None of the words has more than 10 letters, so the first call removes nothing and returns false.
  2. String test. w.startsWith("s") is true for sun and star.
  3. Two removed. The second call returns true.
  4. One left. Only moon remains in the deque.

Example 3 — Use a Named Predicate

Store the condition in a Predicate variable that uses a local limit.

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.function.Predicate;

public class ArrayDequeRemoveIfPredicate {
    public static void main(String[] args) {
        Deque<Integer> ages = new ArrayDeque<>(Arrays.asList(15, 22, 17, 30, 12));
        int limit = 18;
        Predicate<Integer> minor = age -> age < limit;

        boolean changed = ages.removeIf(minor);

        System.out.println(changed + " -> " + ages);
        System.out.println(minor.test(18));
    }
}

How It Works

  1. A captured variable. The lambda uses limit, which is never reassigned, so it counts as effectively final.
  2. Named condition. minor is a Predicate<Integer>; storing it in a variable gives the rule a readable name.
  3. Remove under 18. 15, 17, and 12 match and are removed, leaving [22, 30].
  4. Test it directly. minor.test(18) is false, because 18 is not less than 18.

Frequently Asked Questions

It removes every element for which the given Predicate returns true, and returns true if anything was removed.
Java 8, as a default method of the Collection interface. ArrayDeque has had it ever since.
Yes. It checks every element, wherever it is, and removes all that match.
It throws NullPointerException.
It is shorter and clearer, and newer JDKs implement it efficiently for ArrayDeque. The result is the same.

Next: removeLast()

Remove and return the tail; throws 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.

9 people found this page helpful