Home Java ArrayDeque Methods removeIf() Java ArrayDeque removeIf() Method 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.
Deque<Integer> deque = new ArrayDeque<>(Arrays.asList(1, 2, 3, 4));
boolean changed = deque.removeIf(n -> n % 2 == 0); // true, [1, 3] Call What 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.util.ArrayDeque<E>
public boolean removeIf(Predicate<? super E> filter)
// Declared in Collection<E> (default method since Java 8) Parameters Parameter Description filterRequired. A Predicate that returns true for each element to remove. Must not be null.
Return Value Return Value Description booleantrue if any element was removed; otherwise false.
Compare with Similar Methods Choosing a removal method:
Method How 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 1. Remove even numbers 2. Filter strings 3. Predicate variable Example 1 — Remove All Even Numbers One lambda removes every even number.
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 The filter. n -> n % 2 == 0 is true for 2, 4, and 6.All matches removed. Those three elements are removed in one call.Survivors in order. The odd numbers stay in their original order: [1, 3, 5].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.
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 No long words. None of the words has more than 10 letters, so the first call removes nothing and returns false.String test. w.startsWith("s") is true for sun and star.Two removed. The second call returns true.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.
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 A captured variable. The lambda uses limit, which is never reassigned, so it counts as effectively final.Named condition. minor is a Predicate<Integer>; storing it in a variable gives the rule a readable name.Remove under 18. 15, 17, and 12 match and are removed, leaving [22, 30].Test it directly. minor.test(18) is false, because 18 is not less than 18.Frequently Asked Questions What does ArrayDeque removeIf() do? It removes every element for which the given Predicate returns true, and returns true if anything was removed.
Which Java version added removeIf()? Java 8, as a default method of the Collection interface. ArrayDeque has had it ever since.
Can removeIf() remove elements from the middle of the deque? Yes. It checks every element, wherever it is, and removes all that match.
What happens with removeIf(null)? It throws NullPointerException.
Is removeIf() better than an iterator loop? It is shorter and clearer, and newer JDKs implement it efficiently for ArrayDeque. The result is the same.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion