Skip to content
Related Articles

Related Articles

Improve Article
LinkedBlockingDeque removeFirstOccurrence() method in Java
  • Last Updated : 17 Sep, 2018

The removeFirstOccurrence() method of LinkedBlockingDeque 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.

Below programs illustrate removeFirstOccurrence() method of LinkedBlockingDeque:



Program 1: When element is present




// Java Program to demonstrate removeFirstOccurrence()
// method of LinkedBlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws InterruptedException
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque<Integer> LBD
            = new LinkedBlockingDeque<Integer>();
  
        // Add numbers to end of LinkedBlockingDeque
        LBD.add(15);
        LBD.add(20);
        LBD.add(20);
        LBD.add(15);
  
        // print Dequeue
        System.out.println("Linked Blocking Deque: " + LBD);
  
        if (LBD.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("Linked Blocking Deque: " + LBD);
    }
}
Output:
Linked Blocking Deque: [15, 20, 20, 15]
Fist occurrence of 15 removed
Linked Blocking Deque: [20, 20, 15]

Program 2: When element is not present




// Java Program to demonstrate removeFirstOccurrence()
// method of LinkedBlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws InterruptedException
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque<Integer> LBD
            = new LinkedBlockingDeque<Integer>();
  
        // Add numbers to end of LinkedBlockingDeque
        LBD.add(15);
        LBD.add(20);
        LBD.add(20);
        LBD.add(15);
  
        // print Dequeue
        System.out.println("Linked Blocking Deque: " + LBD);
  
        if (LBD.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("Linked Blocking Deque: " + LBD);
    }
}
Output:
Linked Blocking Deque: [15, 20, 20, 15]
10 not present and not removed
Linked Blocking Deque: [15, 20, 20, 15]

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingDeque.html#removeFirstOccurrence-java.lang.Object-

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 :