Home Java ArrayDeque Methods removeLastOccurrence() Java ArrayDeque removeLastOccurrence() Method Definition In Java, removeLastOccurrence(o) searches an ArrayDeque from the tail toward the head and removes the last element that equals o. It returns true if an element was removed and false otherwise.
It mirrors removeFirstOccurrence(o) . When a value appears only once, both methods remove the same element; they differ only when there are several copies.
An instance method of java.util.ArrayDeque (Java 1.6+): a resizable-array deque with no capacity limit, no null elements, and no thread safety .
Syntax Pass the value to remove.
Deque<String> deque = new ArrayDeque<>(Arrays.asList("a", "b", "a"));
boolean removed = deque.removeLastOccurrence("a"); // true, [a, b] Call What it does deque.removeLastOccurrence(o)Removes the match nearest the tail. deque.removeFirstOccurrence(o)Removes the match nearest the head. deque.removeLast()Removes whatever is at the tail, no search. deque.removeLastOccurrence(null)Returns false.
There is no shorter alias for this method. Like its head-side twin, it searches linearly and returns false for null.
Library declarations // java.util.ArrayDeque<E>
public boolean removeLastOccurrence(Object o)
// Declared in Deque<E> Parameters Parameter Description oRequired. The value to remove, compared with o.equals(element). null never matches.
Return Value Return Value Description booleantrue if an element was removed; false if no element matched.
Compare with Similar Methods How the "remove by value" methods choose what to remove:
Java Examples 1. Last match only 2. First vs last 3. Undo latest like Example 1 — Remove the Last Matching Element Two elements equal "a"; the one nearer the tail is removed.
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
public class ArrayDequeRemoveLastOccDemo {
public static void main(String[] args) {
Deque<String> d = new ArrayDeque<>(Arrays.asList("a", "b", "a", "c"));
boolean removed = d.removeLastOccurrence("a");
System.out.println(removed);
System.out.println(d);
}
} How It Works Search from the tail. The search starts at c and moves toward the head.Last copy found. The "a" in the third position is the first match seen from that side, so it is removed.Head copy stays. The "a" at the head is untouched, giving [a, b, c].Result. The method returns true.Example 2 — removeFirstOccurrence vs removeLastOccurrence Copy one deque, then remove the value 2 from the front side in one copy and from the back side in the other.
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
public class ArrayDequeFirstVsLastOcc {
public static void main(String[] args) {
Deque<Integer> a = new ArrayDeque<>(Arrays.asList(1, 2, 1, 2));
Deque<Integer> b = new ArrayDeque<>(a);
a.removeFirstOccurrence(2);
b.removeLastOccurrence(2);
System.out.println("first: " + a);
System.out.println("last: " + b);
}
} first: [1, 1, 2]
last: [1, 2, 1] How It Works Two equal deques. new ArrayDeque<>(a) copies a, so both start as [1, 2, 1, 2].Front-side removal. In a, the 2 in the second position goes, leaving [1, 1, 2].Back-side removal. In b, the 2 at the tail goes, leaving [1, 2, 1].Boxing again. The int argument 2 is boxed to an Integer and compared with equals.Example 3 — Undo the Most Recent Matching Action An action log grows at the tail. Undoing "like:42" should remove the newest one, not the oldest.
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
public class ArrayDequeUndoLike {
public static void main(String[] args) {
Deque<String> log = new ArrayDeque<>(
Arrays.asList("like:42", "like:7", "share:42", "like:42"));
boolean undone = log.removeLastOccurrence("like:42");
System.out.println(undone + " " + log);
System.out.println(log.removeLastOccurrence("like:99") + " " + log.size());
}
} true [like:42, like:7, share:42]
false 3 How It Works Newest at the tail. The log has two like:42 entries; the newer one is last.Undo the newest. removeLastOccurrence removes the tail entry, so the older like stays in the history.Printed together. true and the shorter log appear on one line through string concatenation.Nothing to undo. like:99 is not in the log, so the call returns false and the size stays 3.Frequently Asked Questions What does ArrayDeque removeLastOccurrence() do? It removes the last element (searching from the tail) that equals the argument, and returns true if one was removed.
How is it different from removeFirstOccurrence()? removeFirstOccurrence removes the copy nearest the head; removeLastOccurrence removes the copy nearest the tail.
Does removeLastOccurrence() remove all copies? No, only one. Loop until it returns false, or use removeIf().
What does removeLastOccurrence(null) return? false. ArrayDeque cannot contain null, and no exception is thrown.
Is it the same as removeLast()? No. removeLast() removes the tail element whatever it is; removeLastOccurrence(o) searches for a specific value.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion