BlockingDeque removeFirstOccurrence() method in Java with Examples
The removeFirstOccurrence() method of BlockingDeque removes the first occurrence of the specified element from this deque. If the deque does not contain the element, it remains unchanged. It returns true if this deque contained the specified element, else it returns false.
Syntax:
public boolean removeFirstOccurrence(Object o)
Parameters: This method accepts a mandatory parameter o which specifies the element to be removed from the Deque container.
Returns: This method returns true if the element is present and removed from the Deque container, else it returns false.
Note: The removeFirstOccurrence() method of BlockingDeque has been inherited from the LinkedBlockingDeque class in Java.
Below programs illustrate removeFirstOccurrence() method of BlockingDeque:
Program 1: When element is present
// Java Program to demonstrate removeFirstOccurrence() // 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(15); BD.add(20); BD.add(20); BD.add(15); // print Deque System.out.println("Blocking Deque: " + BD); if (BD.removeFirstOccurrence(15)) System.out.println("Fist occurrence of 15 removed"); else System.out.println("15 not present and not removed"); // prints the Deque after removal System.out.println("Blocking Deque: " + BD); } } |
Blocking Deque: [15, 20, 20, 15] Fist occurrence of 15 removed Blocking Deque: [20, 20, 15]
Program 2: When element is not present
// Java Program to demonstrate removeFirstOccurrence() // 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(15); BD.add(20); BD.add(20); BD.add(15); // print Deque System.out.println("Blocking Deque: " + BD); if (BD.removeFirstOccurrence(10)) System.out.println("Fist occurrence of 10 removed"); else System.out.println("10 not present and not removed"); // prints the Deque after removal System.out.println("Blocking Deque: " + BD); } } |
Blocking Deque: [15, 20, 20, 15] 10 not present and not removed Blocking Deque: [15, 20, 20, 15]
Recommended Posts:
- BlockingDeque add() method in Java with Examples
- BlockingDeque contains() method in Java with Examples
- BlockingDeque take() method in Java with Examples
- BlockingDeque put() method in Java with Examples
- BlockingDeque putFirst() method in Java with Examples
- BlockingDeque iterator() method in Java with examples
- BlockingDeque offerFirst() method in Java with Examples
- BlockingDeque remove() method in Java with Examples
- BlockingDeque addLast() method in Java with Examples
- BlockingDeque addFirst() method in Java with Examples
- BlockingDeque element() method in java with examples
- BlockingDeque pollLast() method in Java with examples
- BlockingDeque push() method in Java with examples
- BlockingDeque size() method in Java with Examples
- BlockingDeque takeLast() 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.



