Skip to content
Related Articles

Related Articles

BlockingDeque remove() method in Java with Examples
  • Last Updated : 14 Oct, 2019

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:




// 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);
    }
}
Output:
Blocking Deque: [7855642, 35658786, 5278367, 74381793]
Blocking Deque: [35658786, 5278367, 74381793]
Blocking Deque: [35658786, 74381793]

Program 2:




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

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()

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :