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:
// 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); } } |
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()
Recommended Posts:
- BlockingDeque contains() method in Java with Examples
- BlockingDeque put() method in Java with Examples
- BlockingDeque take() method in Java with Examples
- BlockingDeque add() method in Java with Examples
- BlockingDeque addFirst() method in Java with Examples
- BlockingDeque addLast() method in Java with Examples
- BlockingDeque takeLast() method in Java with Examples
- BlockingDeque removeFirstOccurrence() method in Java with Examples
- BlockingDeque size() method in Java with Examples
- BlockingDeque takeFirst() method in Java with Examples
- BlockingDeque removeLastOccurrence() method in Java with Examples
- BlockingDeque putFirst() method in Java with Examples
- BlockingDeque offerFirst() method in Java with Examples
- BlockingDeque offerLast() method in Java with examples
- BlockingDeque pollFirst() method in Java with examples
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.