The Wayback Machine - https://web.archive.org/web/20241009000308/https://www.geeksforgeeks.org/java-8-arraydeque-removeif-method-in-java-with-examples/
Open In App

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

Last Updated : 10 Dec, 2018
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.




// 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);
        }
    }
}


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.




// 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;
    }
}


Output:

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

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




// 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);
        }
    }
}


Output:

Exception: java.lang.NullPointerException

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



Similar Reads

CopyOnWriteArraySet removeIf() method in Java with Examples
The removeIf() method of CopyonWriteArraySet method removes the element from this CopyOnWriteArraySet that satisfies the specified condition. Syntax: public boolean removeIf (Predicate<E> filter) Parameters: This method accepts a mandatory parameter filter which is the predicate value based on which elements are removed from this set. Return
2 min read
LinkedTransferQueue removeIf() method in Java with Examples
The removeIf() method of java.util.concurrent.LinkedTransferQueue is an in-built function is Java which is used to remove all of the elements of this queue that satisfies a given predicate filter which is passed as a parameter to the method. Syntax: public boolean removeIf(Predicate filter) Parameters: This method takes a parameter filter which rep
2 min read
CopyOnWriteArrayList removeIf() method in Java with Examples
The removeIf() method of CopyOnWriteArrayList removes the element from this CopyOnWriteArrayList that satisfies the specified condition. Syntax: public boolean removeIf (Predicate<E> filter) Parameters: This method accepts a mandatory parameter filter which is the predicate value based on which elements are removed from this List. Return Valu
2 min read
LinkedBlockingDeque removeIf() method in Java with Examples
The removeIf() method of LinkedBlockingDeque removes the element from this LinkedBlockingDeque that satisfies the specified condition. Syntax: public boolean removeIf (Predicate<? super E> filter) Parameters: This method accepts a mandatory parameter filter which is the predicate value based on which elements are removed from this Deque. Retu
2 min read
ArrayList removeIf() method in Java
The removeIf() method of ArrayList is used to remove all of the elements of this ArrayList that satisfies a given predicate filter which is passed as a parameter to the method. Errors or runtime exceptions are thrown during iteration or by the predicate are pass to the caller. This method returns True, if we are able to remove some element. Java 8
3 min read
Vector removeIf() method in Java
The removeIf() method of Vector removes all of those elements from Vector which satisfies the condition passed as a parameter to this 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 gi
3 min read
Java Collection removeIf() Method
The removeIf() method of Java Collection removes all the elements of the calling collection that satisfy the given predicate which is passed as a parameter to the function. If the predicate is true, then it removes the item from the calling collection otherwise it keeps the item in the collection. Syntax for removeIf()boolean removeIf(Predicate<
3 min read
ArrayDeque offerLast() Method in Java
The Java.util.ArrayDeque.offerLast(Object element) method in Java is used to add a specific element at the end of this Deque. The function is similar to the addLast(), add() and offer() method of ArrayDeque in Java. Syntax: Array_Deque.offerLast(Object element) Parameters: The parameter element is of the type ArrayDeque and refers to the element to
2 min read
ArrayDeque toArray() Method in Java
The java.util.ArrayDeque.toArray() method is used to form an array of the same elements as that of the Deque. Basically, the method copies all the element from this deque to a new array. Syntax: Object[] arr = Array_Deque.toArray() Parameters: The method does not take any parameters. Return Value: The method returns an array containing the elements
2 min read
ArrayDeque push() Method in Java
The Java.util.ArrayDeque.push(E element) method is used to push an element into the Deque. The operation is similar to the operation in the stack. The element gets pushed onto the top of the deque. Syntax: Array_Deque.push(E element) Parameters: The parameter element is of the type ArrayDeque and refers to the element to be pushed into the deque. R
2 min read
ArrayDeque pop() Method in Java
The Java.util.ArrayDeque.pop() method in Java is used to pop an element from the deque. The element is popped from the top of the deque and is removed from the same. Syntax: Array_Deque.pop() Parameters: The method does not take any parameters. Return Value: This method returns the element present at the front of the Deque. Exceptions: The method t
2 min read
ArrayDeque offer() Method in Java
The Java.util.ArrayDeque.offer(Object element) method in Java is used to add a specific element at the end of the Deque. The function is similar to the offerLast() method of ArrayDeque in Java. Syntax: Array_Deque.offer(Object element) Parameters: The parameter element is of the type ArrayDeque and refers to the element to be added at the end of th
2 min read
ArrayDeque isEmpty() Method in Java
The Java.util.ArrayDeque.isEmpty() method in Java is used to check and verify if an ArrayDeque is empty or not. It returns True if the Deque is empty else it returns False. Syntax: Array_Deque.isEmpty() Parameters: The method does not take any parameter. Return Value: The function returns True if the deque is empty else it returns False. Below prog
2 min read
ArrayDeque getLast() Method in Java
The java.util.ArrayDeque.getLast() method in Java is used to retrieve or fetch the last element of the ArrayDeque. In the process, the method does not delete the element from the deque instead it just returns the last element of the deque.Syntax: Array_Deque.getLast() Parameters: The method does not take any parameter.Return Value: The method retur
2 min read
ArrayDeque getFirst() Method in Java
The java.util.ArrayDeque.getFirst() method in Java is used to retrieve or fetch the first element of the ArrayDeque. In the process, the method does not delete the element from the deque instead it just returns the first element of the deque. Syntax: Array_Deque.getFirst() Parameters: The method does not take any parameter. Return Value: The method
2 min read
ArrayDeque clone() Method in Java
The Java.util.ArrayDeque.clone() method is used to return a shallow copy of this deque. It just creates a copy of the deque. Syntax: Array_Deque.clone() Parameters: The method does not take any parameter. Return Value: The method just returns a copy of the ArrayDeque. Below programs illustrate the Java.util.ArrayDeque.clone() method: Program 1: //
2 min read
ArrayDeque contains() Method in Java
The Java.util.ArrayDeque.contains() method in Java is used to check or verify whether a specific element is present in the Deque or not. Syntax: Array_Deque.contains(Object element) Parameters: The parameter element is of the type of ArrayDeque. This is the element that needs to be tested if it is present in the deque or not. Return Value: The meth
2 min read
ArrayDeque element() Method in Java
The java.util.ArrayDeque.element() method in Java is used to retrieve or fetch the head of the ArrayDeque. In the process, the method does not delete the element from the deque instead it just returns the element. Syntax: Array_Deque.element() Parameters: The method does not take any parameter. Return Value: The method returns the element present a
2 min read
ArrayDeque add() Method in Java
The Java.util.ArrayDeque.add(Object element) method in Java is used to add a specific element at the end of the Deque. The function is similar to the addLast() method of ArrayDeque in Java. Syntax: Array_Deque.add(Object element) Parameters: The parameter element is of the type ArrayDeque and refers to the element to be added to the Deque. Return V
2 min read
ArrayDeque clear() Method in Java
The Java.util.ArrayDeque.clear() method in Java is used to remove all of the elements from the Deque. Using the clear() method only clears all the element from the deque and does not delete the deque. In other words, it can be said that the clear() method is used to only empty an existing ArrayDeque. Syntax: Array_Deque.clear() Parameters: The meth
2 min read
ArrayDeque size() Method in Java
The Java.util.ArrayDeque.size() method in Java is used to get the size of the Deque or the number of elements present in the Deque. Syntax: Array_Deque.size() Parameters: The method does not take any parameter. Return Value: The method returns the size or the number of elements present in the Deque. Below programs illustrate the Java.util.ArrayDequ
2 min read
ArrayDeque addFirst() Method in Java
The java.util.ArrayDeque.addFirst(Object element) method in Java is used to insert a specific element at the front of this deque. Syntax: Array_Deque.addFirst(Object element) Parameters: The parameter element is of the type ArrayDeque and refers to the element to be added. Return Value: The function does not return any value. Exceptions: The method
2 min read
ArrayDeque addLast() Method in Java
The java.util.ArrayDeque.addLast(Object element) method in Java is used to insert a specific element at the end of this deque. It is similar to the add() method in Java. Syntax: Array_Deque.addLast(Object element) Parameters: The parameter element is of the type ArrayDeque and refers to the element to be added. Return Value: The function does not r
2 min read
ArrayDeque offerFirst() Method in Java
The Java.util.ArrayDeque.offerFirst(Object element) method in Java is used to add a specific element at the front of the Deque. The function is similar to the addFirst() method of ArrayDeque in Java. Syntax: Array_Deque.offerFirst(Object element) Parameters: The parameter element is of the type ArrayDeque and refers to the element to be added at th
2 min read
ArrayDeque poll() Method in Java
The java.util.ArrayDeque.poll() method in Java is used to retrieve or fetch and remove the element present at the head of the Deque. The peek() method only retrieved the element at the head but the poll() also removes the element along with the retrieval. It returns NULL if the deque is empty. Syntax: Array_Deque.poll() Parameters: The method does
2 min read
ArrayDeque descendingIterator() Method in Java
The Java.util.ArrayDeque.descendingIterator() method is used to return an iterator of the same elements as the ArrayDeque but in the reverse order. Syntax: Iterator iterate_value = Array_Deque.descendingIterator(); Parameters: The method does not take any parameter. Return Value: The method iterates over the elements of the deque and returns the va
2 min read
ArrayDeque iterator() Method in Java
The Java.util.ArrayDeque.iterator() method is used to return an iterator of the elements of the ArrayDeque. Syntax: Iterator iterate_value = Array_Deque.iterator(); Parameters: The method does not take any parameter. Return Value: The method iterates over the elements of the deque and returns the values(iterator). Below programs illustrate the Java
2 min read
ArrayDeque peekFirst() Method in Java
The java.util.ArrayDeque.peekFirst() method in Java is used to retrieve or fetch the first element of the deque. The element retrieved does not get deleted or removed from the Queue instead the method just returns it. If no element is present in the deque or it is empty, then Null is returned. Syntax: Array_Deque.peekFirst() Parameters: The method
2 min read
ArrayDeque peek() Method in Java
The java.util.ArrayDeque.peek() method in Java is used to retrieve or fetch the element at the head of the Deque. The element retrieved does not get deleted or removed from the Queue instead the method just returns it. If no element is present in the deque then Null is returned. Syntax: Array_Deque.peek() Parameters: The method does not take any pa
2 min read
ArrayDeque peekLast() Method in Java
The java.util.ArrayDeque.peekLast() method in Java is used to retrieve or fetch the last element of the deque. The element retrieved does not get deleted or removed from the Queue instead the method just returns it. If no element is present in the deque or it is empty, then Null is returned. Syntax: Array_Deque.peekLast() Parameters: The method doe
2 min read