Thursday, October 16, 2025
HomeLanguagesJavaLinkedBlockingDeque removeIf() method in Java with Examples

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:




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


Output:

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

Program 2:




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


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-

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS