The removeIf() method of ArrayDeque removes all elements that satisfy a specified condition. It uses a Predicate to test each element and returns true if one or more elements are removed.
- The method was introduced as a default method in the Collection interface in Java 8.
- It removes all elements for which the specified Predicate returns true.
- A NullPointerException is thrown if the specified predicate is null.
Syntax
public boolean removeIf(Predicate<? super E> filter)
The method returns true if one or more elements are removed; otherwise, it returns false.
The method throws NullPointerException when the specified filter is null.
Ways to Use removeIf() in ArrayDeque
1: Using removeIf() to Remove Strings Based on a Condition
In this program, we use an ArrayDeque of strings and removes all elements whose names start with the letter A. The predicate name -> name.startsWith("A") is applied to each element.
import java.util.ArrayDeque;
public class GFG {
public static void main(String[] args) {
ArrayDeque<String> students = new ArrayDeque<>();
students.add("Aman");
students.add("Sanjeet");
students.add("Amar");
students.add("Rabi");
students.add("Shabbir");
students.removeIf(name -> name.startsWith("A"));
System.out.println("Students after removal:");
for (String name : students) {
System.out.println(name);
}
}
}
Output
Students after removal: Sanjeet Rabi Shabbir
Explanation: The removeIf() method removes every name that satisfies the predicate, so "Aman" and "Amar" are removed from the deque.
2. Using removeIf() with Custom Objects
In this program, we use an ArrayDeque of Student objects and remove all students whose marks are below 40. The predicate student -> student.marks < 40 is applied to each element.
import java.util.ArrayDeque;
class Student {
String name;
int marks;
Student(String name, int marks) {
this.name = name;
this.marks = marks;
}
}
public class GFG {
public static void main(String[] args) {
ArrayDeque<Student> students = new ArrayDeque<>();
students.add(new Student("Aman", 78));
students.add(new Student("Amar", 79));
students.add(new Student("Suraj", 38));
students.add(new Student("Raju", 22));
students.add(new Student("Ankit", 76));
students.add(new Student("Sanju", 62));
students.removeIf(student -> student.marks < 40);
System.out.println("Students with marks 40 or above:");
for (Student student : students) {
System.out.println(student.name + ", " + student.marks);
}
}
}
Output
Students with marks 40 or above: Aman, 78 Amar, 79 Ankit, 76 Sanju, 62
Explanation: The predicate checks each student's marks, and students scoring below 40 are removed.
3: Handling a Null Predicate
In this program, we pass null as the predicate to the removeIf() method. Since the specified predicate cannot be null, the method throws a NullPointerException.
import java.util.ArrayDeque;
public class GFG {
public static void main(String[] args) {
ArrayDeque<String> students = new ArrayDeque<>();
students.add("Aman");
students.add("Sanjeet");
students.add("Amar");
try {
students.removeIf(null);
} catch (NullPointerException e) {
System.out.println("Exception: " + e);
}
}
}
Output
Exception: java.lang.NullPointerException
Explanation: Passing null as the filter causes removeIf() to throw a NullPointerException.