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.

Returns: This method returns a boolean value such as true, if the LinkedBlockingDeque is changed. Else this method returns false.

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

Below program illustrates the removeIf() function of LinkedBlockingDeque class :

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate removeIf()
// method of LinkedBlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws InterruptedException
    {
  
        // Create object of LinkedBlockingDeque
        LinkedBlockingDeque<Integer> LBD
            = new LinkedBlockingDeque<Integer>();
  
        // Add numbers to end of LinkedBlockingDeque
        LBD.add(7855642);
        LBD.add(35658786);
        LBD.add(5278367);
        LBD.add(74381793);
  
        // Print Deque
        System.out.println("Linked Blocking Deque: "
                           + LBD);
  
        // If a number in the List is
        // divisible by 3, then remove it
        LBD.removeIf(number -> number % 3 == 0);
  
        System.out.println("Linked Blocking Deque: "
                           + LBD);
    }
}

chevron_right


Output:

Linked Blocking Deque: [7855642, 35658786, 5278367, 74381793]
Linked Blocking Deque: [7855642, 5278367]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate removeIf()
// method of LinkedBlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws InterruptedException
    {
  
        // Create object of LinkedBlockingDeque
        LinkedBlockingDeque<Integer> LBD
            = new LinkedBlockingDeque<Integer>();
  
        // Add numbers to end of LinkedBlockingDeque
        LBD.add(7855642);
        LBD.add(35658786);
        LBD.add(5278367);
        LBD.add(74381793);
  
        // Print Dequeue
        System.out.println("Linked Blocking Deque: "
                           + LBD);
  
        try {
            // if the predicate is null,
            // then it will throw NullPointerException
            LBD.removeIf(null);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

chevron_right


Output:

Linked Blocking Deque: [7855642, 35658786, 5278367, 74381793]
java.lang.NullPointerException

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/LinkedBlockingDeque.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.