Java Utility Library

Java ArrayDeque - removeLastOccurrence() Method



The java.util.ArrayDeque.removeLastOccurrence() method is used to remove the last 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 removeLastOccurrence(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.removeLastOccurrence() method is used to remove the last 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(40);

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

    //remove the last occurrence of 15
    MyDeque.removeLastOccurrence(15);

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

The output of the above code will be:

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

❮ Java.util - ArrayDeque