BlockingDeque remove() method in Java with Examples

The remove() method of BlockingDeque removes the head of the Deque container. The method throws a NoSuchElementException if the Deque container is empty. If an element in passed in the parameter, it removes the given element if present in the Deque.

Syntax:

public E remove() or boolean remove(element)

Parameters: This method accepts a temporary parameter element which is to be removed from the BlockingDeque



Returns: This method returns true or false if the parameter is passed, else it returns nothing. It returns true if the element is there in the Deque, or else it returns false.

Exception: The function throws a NoSuchElementException if the Deque is empty.

Note: The remove() method of BlockingDeque has been inherited from the LinkedBlockingDeque class in Java.

Below programs illustrate remove() method of BlockingDeque:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate remove()
// method of BlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws InterruptedException
    {
  
        // create object of BlockingDeque
        BlockingDeque<Integer> BD
            = new LinkedBlockingDeque<Integer>();
  
        // Add numbers to end of BlockingDeque
        BD.add(7855642);
        BD.add(35658786);
        BD.add(5278367);
        BD.add(74381793);
  
        // print Deque
        System.out.println("Blocking Deque: " + BD);
  
        // removes the front element
        BD.remove();
        System.out.println("Blocking Deque: " + BD);
  
        // removes the element
        BD.remove(5278367);
        System.out.println("Blocking Deque: " + BD);
    }
}

chevron_right


Output:

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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate remove()
// method of BlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws InterruptedException
    {
  
        // create object of BlockingDeque
        BlockingDeque<Integer> BD
            = new LinkedBlockingDeque<Integer>();
  
        // Add numbers to end of BlockingDeque
        BD.add(7855642);
        BD.add(35658786);
        BD.add(5278367);
        BD.add(74381793);
  
        // print Deque
        System.out.println("Blocking Deque: " + BD);
  
        BD.clear();
  
        // removes the front element
        BD.remove();
        System.out.println("Blocking Deque: " + BD);
  
        // removes the element
        BD.remove(5278367);
        System.out.println("Blocking Deque: " + BD);
    }
}

chevron_right


Output:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.concurrent.LinkedBlockingDeque.removeFirst(LinkedBlockingDeque.java:453)
    at java.util.concurrent.LinkedBlockingDeque.remove(LinkedBlockingDeque.java:672)
    at GFG.main(GFG.java:29)

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingDeque.html#remove()



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

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.