Java ArrayDeque removeFirstOccurrence() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

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.

Java
Deque<String> deque = new ArrayDeque<>(Arrays.asList("a", "b", "a"));
boolean removed = deque.removeFirstOccurrence("a");   // true, [b, a]
CallWhat 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
// java.util.ArrayDeque<E>
public boolean removeFirstOccurrence(Object o)

// Declared in Deque<E>

Parameters

ParameterDescription
oRequired. The value to remove, compared with o.equals(element). null is allowed and never matches.

Return Value

Return ValueDescription
booleantrue if an element was removed; false if no element matched.

Compare with Similar Methods

Methods that remove by value:

MethodHow it differs
remove(o)Identical behavior; shorter name from Collection.
removeLastOccurrence(o)Searches from the tail and removes the last match.
removeAll(c)Removes every element that is in another collection.
removeIf(filter)Removes every element that matches a condition.

Java Examples

Example 1 — Remove the First Matching Element

Two elements equal "a"; only the one nearer the head is removed.

Java
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

  1. Two copies. "a" appears at the head and again in the third position.
  2. Search from the head. The first "a" found is the head, and that one is removed.
  3. Report. The method returns true because something was removed.
  4. 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.

Java
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

  1. Missing value. "z" is not in the deque, so the call returns false.
  2. null is tolerated. Unlike the add methods, removeFirstOccurrence(null) does not throw; it just returns false.
  3. Unchanged. Nothing matched, so the deque still prints [x, y].
  4. 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.

Java
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);
        }
    }
}

How It Works

  1. Three Sams. The name appears at the head, in the middle, and at the tail.
  2. Loop on the result. Each call removes the Sam nearest the head and returns true, so the loop body runs.
  3. Shrinking list. After three removals only Ivy and Leo are left, in their original order.
  4. Exit. The fourth call finds no Sam and returns false, ending the loop.

Frequently Asked Questions

It removes the first element, searching from the head, that equals the argument, and returns true if one was removed.
Yes. For ArrayDeque, remove(o) simply calls removeFirstOccurrence(o).
No, only the first one. Loop until it returns false, or use removeIf() to remove every match.
It returns false. It does not throw an exception.
removeFirst() removes whatever is at the head. removeFirstOccurrence(o) searches for a specific value anywhere in the deque.

Next: removeIf()

Remove every element that matches a condition.

removeIf() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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