Java Utility Library

Java ArrayDeque - removeFirstOccurrence() Method



The java.util.ArrayDeque.removeFirstOccurrence() method is used to remove the first occurrence of the specified element from the deque (when traversing the deque from head to tail). If the deque does not contain the element, it will be unchanged.

Syntax

public boolean removeFirstOccurrence(Object obj)

Parameters

obj Specify the element to be removed from this deque, if present.

Return Value

Returns true if the deque contained the specified element.

Exception

NA.

Example:

In the example below, the java.util.ArrayDeque.removeFirstOccurrence() method is used to remove the first occurrence of 15 from the given deque.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating an ArrayDeque
    ArrayDeque<Integer> MyDeque = new ArrayDeque<Integer>();

    //populating the ArrayDeque
    MyDeque.add(10);
    MyDeque.add(15);
    MyDeque.add(30);
    MyDeque.add(15);
    MyDeque.add(15);

    //printing the ArrayDeque
    System.out.println("MyDeque contains: " + MyDeque);

    //remove the first occurrence of 15
    MyDeque.removeFirstOccurrence(15);

    //printing ArrayDeque
    System.out.println("MyDeque contains: " + MyDeque);
  }
}

The output of the above code will be:

MyDeque contains: [10, 15, 30, 15, 15]
MyDeque contains: [10, 30, 15, 15]

❮ Java.util - ArrayDeque