Java 8 | ArrayDeque removeIf() method in Java with Examples

The removeIf() method of ArrayDeque is used to remove all those elements from ArrayDeque which satisfies a given predicate filter condition passed as a parameter to the method. This method returns true if some element are removed from the Vector.

Java 8 has an important in-built functional interface which is Predicate. Predicate, or a condition checking function, which checks the given input for a given condition and returns a boolean result for the same indicating whether the condition was met or not.

Syntax:



public boolean removeIf(Predicate<? super E> filter)

Parameter: This method takes a parameter filter which represents a predicate which returns true for elements to be removed.

Returns: This method returns True if predicate returns true and some elements were removed.

Exception: This method throws NullPointerException if the specified filter is null.

Below programs illustrate removeIf() method of ArrayDeque:

Example 1: To demonstrate removeIf() method on ArrayDeque which contains a set of String and remove strings starts with A.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate removeIf()
// method of ArrayDeque
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ArrayDeque
        // containing a list of string values
        ArrayDeque<String> students = new ArrayDeque<String>();
  
        // Add Strings to list
        // each string represents student name
        students.add("Aman");
        students.add("Sanjeet");
        students.add("Amar");
        students.add("Rabi");
        students.add("Shabbir");
  
        // apply removeIf() method
        // to remove names which start with A
        students.removeIf(n -> (n.charAt(0) == 'A'));
  
        System.out.println("Students name do not starts with A");
  
        // print list
        for (String str : students) {
            System.out.println(str);
        }
    }
}

chevron_right


Output:

Students name do not starts with A
Sanjeet
Rabi
Shabbir

Example 2: To demonstrate removeIf() method on ArrayDeque which contains set of Students objects to remove all those students who got less than 40 marks.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate removeIf()
// method of ArrayDeque
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ArrayDeque
        // containing a list of Student objects
        ArrayDeque<student> students = new ArrayDeque<student>();
  
        // Add student object to list
        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));
  
        // apply removeIf() method
        // to remove students who scores below 40
        students.removeIf(n -> (n.marks <= 40));
  
        System.out.println("Students list who score above 40");
  
        // print list
        for (student str : students) {
            System.out.println(str.name + ", " + str.marks);
        }
    }
}
  
// create student class
class student {
  
    public String name;
    public int marks;
  
    student(String name, int marks)
    {
        this.name = name;
        this.marks = marks;
    }
}

chevron_right


Output:

Students list who score above 40
Aman, 78
Amar, 79
Ankit, 76
Sanju, 62

Example 3: To demonstrate NullpointerException in removeIf() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate removeIf()
// method of ArrayDeque
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ArrayDeque
        // containing a list of string values
        ArrayDeque<String> students = new ArrayDeque<String>();
  
        // Add Strings to list
        // each string represents student name
        students.add("Aman");
        students.add("Sanjeet");
        students.add("Amar");
        students.add("Rabi");
        students.add("Shabbir");
  
        try {
            // apply removeIf() method with null filter
            students.removeIf(null);
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}

chevron_right


Output:

Exception: java.lang.NullPointerException

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/ArrayDeque.html#removeIf(java.util.function.Predicate)




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.