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:
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
BlockingDeque<Integer> BD
= new LinkedBlockingDeque<Integer>();
BD.add( 7855642 );
BD.add( 35658786 );
BD.add( 5278367 );
BD.add( 74381793 );
System.out.println( "Blocking Deque: " + BD);
BD.remove();
System.out.println( "Blocking Deque: " + BD);
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:
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
BlockingDeque<Integer> BD
= new LinkedBlockingDeque<Integer>();
BD.add( 7855642 );
BD.add( 35658786 );
BD.add( 5278367 );
BD.add( 74381793 );
System.out.println( "Blocking Deque: " + BD);
BD.clear();
BD.remove();
System.out.println( "Blocking Deque: " + BD);
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()
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...