Java ArrayDeque remove() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, ArrayDeque has two remove methods. remove() with no argument takes the head off the deque and returns it, just like removeFirst(). If the deque is empty, it throws NoSuchElementException.

remove(Object o) searches from the head and removes the first element equal to o (compared with equals). It returns true if something was removed and false otherwise. It is the same as removeFirstOccurrence(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

Call it with no argument to take the head, or with a value to remove that value.

Java
Deque<String> deque = new ArrayDeque<>(Arrays.asList("a", "b", "a"));
String head = deque.remove();         // "a", deque = [b, a]
boolean gone = deque.remove("a");     // true, deque = [b]
CallWhat it does
deque.remove()Removes and returns the head. Throws on an empty deque.
deque.remove(o)Removes the first element equal to o; returns true or false.
deque.poll()Like remove(), but returns null when empty.
deque.remove(null)Returns false; an ArrayDeque never contains null.

remove() runs in constant time. remove(Object) has to search, so it takes time proportional to the size of the deque. A Deque has no remove(int index) method, so remove(5) on a Deque<Integer> removes the value 5.

Library declarations
Java
// java.util.ArrayDeque<E>
public E remove()                  // same as removeFirst()
public boolean remove(Object o)    // same as removeFirstOccurrence(o)

// Declared in Queue<E>, Collection<E>, and Deque<E>

Parameters

ParameterDescription
(none)remove() takes no argument and works on the head.
oremove(Object o) only. The value to remove. Matching uses o.equals(element).

Return Value

Return ValueDescription
Eremove(): the element that was at the head.
booleanremove(o): true if an element was removed, false if no match was found.

Throw or Return a Special Value?

Each end has a method that throws when it cannot work (such as on an empty deque) and one that returns null or false instead. The highlighted cell matches remove().

OperationHead: throwsHead: special valueTail: throwsTail: special value
InsertaddFirst()offerFirst()addLast()offerLast()
RemoveremoveFirst()pollFirst()removeLast()pollLast()
ExaminegetFirst()peekFirst()getLast()peekLast()

Queue and stack names: add() = addLast(), offer() = offerLast(), push() = addFirst(), pop() = removeFirst(), remove() = removeFirst(), poll() = pollFirst(), element() = getFirst(), peek() = peekFirst().

Java Examples

Example 1 — Remove the Head of a Queue

remove() takes the first element and returns it.

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;

public class ArrayDequeRemoveDemo {
    public static void main(String[] args) {
        Queue<String> q = new ArrayDeque<>(Arrays.asList("A", "B", "C"));

        String head = q.remove();

        System.out.println("removed " + head);
        System.out.println("queue " + q);
        System.out.println("next " + q.element());
    }
}

How It Works

  1. Three letters. The queue starts as [A, B, C] with A at the head.
  2. Take the head. remove() unlinks A and returns it into head.
  3. The rest moves up. B is now the head, and the queue prints [B, C].
  4. Check without removing. element() reads the new head, B, and leaves it in place.

Example 2 — remove(Object) and the Integer Pitfall

On a Deque<Integer>, remove(5) means "remove the value 5", not "remove index 5".

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class ArrayDequeRemoveValue {
    public static void main(String[] args) {
        Deque<Integer> nums = new ArrayDeque<>(Arrays.asList(5, 1, 5, 2));

        System.out.println(nums.remove(5));
        System.out.println(nums);
        System.out.println(nums.remove(9));
        System.out.println(nums);
    }
}

How It Works

  1. Boxing picks the overload. Deque has no method that takes an int, so Java boxes 5 to an Integer and calls remove(Object).
  2. Only the first 5. The search starts at the head, finds the 5 in front, removes it, and returns true. The second 5 stays.
  3. No match. There is no 9 in the deque, so remove(9) returns false and nothing changes.
  4. Different from List. On a List<Integer>, remove(5) would call remove(int index) instead. A deque has no index-based remove.

Example 3 — remove() on an Empty Deque

Remove the only element, then call remove() again on the empty deque, and try to remove a value that is not there.

Java
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.NoSuchElementException;

public class ArrayDequeRemoveEmpty {
    public static void main(String[] args) {
        Deque<String> d = new ArrayDeque<>();
        d.add("only");
        System.out.println(d.remove());

        try {
            d.remove();
        } catch (NoSuchElementException e) {
            System.out.println("remove(): " + e.getClass().getSimpleName());
        }
        System.out.println("remove(\"ghost\"): " + d.remove("ghost"));
    }
}

How It Works

  1. One element out. The first remove() returns "only" and leaves the deque empty.
  2. Nothing left to take. The second remove() cannot return a head, so it throws NoSuchElementException.
  3. The value form never throws for a miss. remove("ghost") simply reports false, even on an empty deque.
  4. Escaped quotes. The \" in the label prints real double quotes around ghost in the output.

Frequently Asked Questions

remove() with no argument removes and returns the head of the deque. It throws NoSuchElementException if the deque is empty.
It removes the first element (nearest the head) that equals the given object and returns true, or returns false if there is no match.
Both remove the head. remove() throws NoSuchElementException on an empty deque; poll() returns null.
No. A Deque has no index-based remove, so 5 is boxed to an Integer and remove(Object) removes the value 5.
No, only the first one. Use removeIf() or removeAll() to remove every match.

Next: removeAll()

Remove every element that is in another collection.

removeAll() 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.

14 people found this page helpful