Saturday, September 28, 2024
Google search engine
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

Recent Comments