ArrayDeque remove Last Occurrence() Method in Java
The java.util.ArrayDeque.removeLastOccurrence(Object) method in Java is used to remove the last occurrence of a particular element from the deque. If the element is exists only once, it is removed, and a boolean value of true indicates successful removal. If no such element is removed, the method returns false, indicating failure.
Syntax:
ArrDeque.removeLastOccurrence(object Ob)
Parameters:
The method accepts one parameter named ob of ArrDeque, which represents the element whose last occurrence is to be removed.
Return type: The method returns true if the element exists in the deque, else it returns false.
Example 1:
The use of the Java ArrayDeque removeLastOcurrence() method with Strings is demonstrated in the example below. We are constructing an ArrayDeque of Strings, adding some elements, and Removing the elements using the removeLastOccurrence() method. After removing the remaining elements are printed in the output.
FileName: ArrayDequeRemoveLastOccurrenceExample1.java
// importing util packages import java.util.*; public class ArrayDequeRemoveLastOccurrenceExample1 { // main method public static void main(String args[]) { // Creating an empty dequeArr DequedequeArr = new ArrayDeque (); // adding elements into the Deque using the add() method dequeArr.add("Welcome"); dequeArr.add("To"); dequeArr.add("Java"); dequeArr.add("Online"); dequeArr.add("Tutorials"); // Displaying the ArrayDeque System.out.println("Initial ArrayDeque is: " + dequeArr); // Removing the elements using removeLastOccurrence() method dequeArr.removeLastOccurrence("Online"); dequeArr.removeLastOccurrence("Java"); // Displaying the ArrayDeque after removal of specific elements System.out.println("The ArrayDeque after removing specific elements: " + dequeArr); } }
Output:
Initial ArrayDeque is: [Welcome, To, Java, Online, Tutorials] The ArrayDeque after removing specific elements: [Welcome, To, Tutorials]
Program 2:
The use of the Java ArrayDeque removeLastOcurrence() method with Integers is demonstrated in the example below. We are constructing an ArrayDeque of integer numbers, then adding some elements into it, and after that, removing the elements using the removeLastOccurrence() method. After removing, the remaining elements are printed.
FileName: ArrayDequeRemoveLastOccurrenceExample2.java
// importing util packages import java.util.*; public class ArrayDequeRemoveLastOccurrenceExample2 { // main method public static void main(String args[]) { // Creating an empty dequeArr DequedequeArr = new ArrayDeque (); // adding elements into the Deque using the add() method dequeArr.add(13); dequeArr.add(16); dequeArr.add(30); dequeArr.add(26); dequeArr.add(4); dequeArr.add(32); // displaying the ArrayDeque System.out.println("Initial ArrayDeque is: " + dequeArr); // removing the elements using removeLastOccurrence() method dequeArr.removeLastOccurrence(32); dequeArr.removeLastOccurrence(16); // Displaying the ArrayDeque after removal of specific elements System.out.println("The ArrayDeque after removing specific elements: " + dequeArr); } }
Output:
Initial ArrayDeque is: [13, 16, 30, 26, 4, 32] The ArrayDeque after removing specific elements: [13, 30, 26, 4]