Home Java ArrayDeque Methods removeFirstOccurrence() Java ArrayDeque removeFirstOccurrence() Method Definition In Java, removeFirstOccurrence(o) searches an ArrayDeque from the head toward the tail and removes the first element that equals o. It returns true if an element was removed and false if there was no match.
It is exactly what remove(Object o) does. Other copies of the same value stay in the deque. To search from the tail instead, use removeLastOccurrence(o) .
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.removeFirstOccurrence("a"); // true, [b, a] Call What it does deque.removeFirstOccurrence(o)Removes the first match from the head side. deque.remove(o)Same operation. deque.removeLastOccurrence(o)Removes the match nearest the tail. deque.removeFirstOccurrence(null)Returns false; no exception.
The search is linear, and removing from the middle means shifting some elements, so the method takes time proportional to the size of the deque.
Library declarations // java.util.ArrayDeque<E>
public boolean removeFirstOccurrence(Object o)
// Declared in Deque<E> Parameters Parameter Description oRequired. The value to remove, compared with o.equals(element). null is allowed and never matches.
Return Value Return Value Description booleantrue if an element was removed; false if no element matched.
Compare with Similar Methods Methods that remove by value:
Java Examples 1. First match only 2. No match 3. Remove every copy Example 1 — Remove the First Matching Element Two elements equal "a"; only the one nearer the head is removed.
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
public class ArrayDequeRemoveFirstOccDemo {
public static void main(String[] args) {
Deque<String> d = new ArrayDeque<>(Arrays.asList("a", "b", "a", "c"));
boolean removed = d.removeFirstOccurrence("a");
System.out.println(removed);
System.out.println(d);
}
} How It Works Two copies. "a" appears at the head and again in the third position.Search from the head. The first "a" found is the head, and that one is removed.Report. The method returns true because something was removed.The other copy stays. The deque is now [b, a, c].Example 2 — No Match and null Return false Neither a missing value nor null causes an exception.
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
public class ArrayDequeRemoveFirstOccMiss {
public static void main(String[] args) {
Deque<String> d = new ArrayDeque<>(Arrays.asList("x", "y"));
System.out.println(d.removeFirstOccurrence("z"));
System.out.println(d.removeFirstOccurrence(null));
System.out.println(d);
}
} How It Works Missing value. "z" is not in the deque, so the call returns false.null is tolerated. Unlike the add methods, removeFirstOccurrence(null) does not throw; it just returns false.Unchanged. Nothing matched, so the deque still prints [x, y].Use the boolean. The return value tells you whether the deque changed, without comparing sizes.Example 3 — Remove All Copies One by One Call the method in a while loop until it returns false, printing the deque after each removal.
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
public class ArrayDequeRemoveFirstOccLoop {
public static void main(String[] args) {
Deque<String> waitlist = new ArrayDeque<>(Arrays.asList("Sam", "Ivy", "Sam", "Leo", "Sam"));
int removed = 0;
while (waitlist.removeFirstOccurrence("Sam")) {
removed++;
System.out.println("after " + removed + ": " + waitlist);
}
}
} after 1: [Ivy, Sam, Leo, Sam]
after 2: [Ivy, Leo, Sam]
after 3: [Ivy, Leo] How It Works Three Sams. The name appears at the head, in the middle, and at the tail.Loop on the result. Each call removes the Sam nearest the head and returns true, so the loop body runs.Shrinking list. After three removals only Ivy and Leo are left, in their original order.Exit. The fourth call finds no Sam and returns false, ending the loop.Frequently Asked Questions What does ArrayDeque removeFirstOccurrence() do? It removes the first element, searching from the head, that equals the argument, and returns true if one was removed.
Is removeFirstOccurrence() the same as remove(Object)? Yes. For ArrayDeque, remove(o) simply calls removeFirstOccurrence(o).
Does it remove all matching elements? No, only the first one. Loop until it returns false, or use removeIf() to remove every match.
What happens with removeFirstOccurrence(null)? It returns false. It does not throw an exception.
How is it different from removeFirst()? removeFirst() removes whatever is at the head. removeFirstOccurrence(o) searches for a specific value anywhere in the deque.
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.
8 people found this page helpful
Helpful Share Copy link Suggestion