Java ArrayDeque removeFirst() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, removeFirst() removes the head (first element) of an ArrayDeque and returns it. The next element becomes the new head.

If the deque is empty, it throws NoSuchElementException. Its "special value" twin, pollFirst(), returns null instead. pop() and remove() do exactly the same as removeFirst().

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. Since Java 21, removeFirst is also declared by the SequencedCollection interface, which Deque extends.

Syntax

Call removeFirst with no arguments and use the returned element.

Java
Deque<Integer> deque = new ArrayDeque<>(Arrays.asList(10, 20, 30));
int first = deque.removeFirst();    // 10, deque = [20, 30]
CallWhat it does
deque.removeFirst()Removes and returns the head; throws if empty.
deque.pollFirst()Same, but returns null if empty.
deque.pop()Same as removeFirst(); stack-style name.
deque.remove()Same as removeFirst(); queue-style name.

Use removeFirst when an empty deque would be a bug in your program: the exception points straight at the problem. When an empty deque is a normal situation, check isEmpty() first or use pollFirst().

Library declarations
Java
// java.util.ArrayDeque<E>
public E removeFirst()

// Declared in Deque<E> (and SequencedCollection<E> since Java 21)

Parameters

ParameterDescription
NoneremoveFirst() takes no parameters.

Return Value

Return ValueDescription
EThe element that was at the head. Never null, because an ArrayDeque cannot hold null.

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

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 First Element

Remove the head of a number deque and look at what is left.

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

public class ArrayDequeRemoveFirstDemo {
    public static void main(String[] args) {
        Deque<Integer> d = new ArrayDeque<>(Arrays.asList(10, 20, 30));

        int first = d.removeFirst();

        System.out.println("removed: " + first);
        System.out.println("now:     " + d);
        System.out.println("head:    " + d.getFirst());
    }
}

How It Works

  1. Head is 10. The deque is built from the list, so 10 is first.
  2. Removed and unboxed. removeFirst() returns the Integer 10, which is unboxed into the int variable first.
  3. Shorter deque. Two elements remain: [20, 30].
  4. A new head. getFirst() now returns 20.

Example 2 — Running Past the End Throws

A loop that keeps calling removeFirst() ends with an exception. This shows the behavior; in real code, check isEmpty() instead.

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

public class ArrayDequeRemoveFirstLoop {
    public static void main(String[] args) {
        Deque<String> d = new ArrayDeque<>(Arrays.asList("x", "y"));
        int processed = 0;

        try {
            while (true) {
                String item = d.removeFirst();
                System.out.println("processing " + item);
                processed++;
            }
        } catch (NoSuchElementException e) {
            System.out.println("stopped after " + processed + " items");
        }
    }
}

How It Works

  1. An endless loop on purpose. while (true) has no exit condition of its own.
  2. Two successful removals. x and then y are removed and printed, and processed reaches 2.
  3. The third call fails. The deque is empty, so removeFirst() throws NoSuchElementException and control jumps to the catch block.
  4. Better style. Using exceptions to end a loop is slow and hard to read; while (!d.isEmpty()) says the same thing clearly.

Example 3 — removeFirst, pop, and remove Do the Same Thing

Call all three on one deque in a single expression and see that each removes the current head.

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

public class ArrayDequeRemoveFirstAliases {
    public static void main(String[] args) {
        Deque<Integer> d = new ArrayDeque<>(Arrays.asList(1, 2, 3, 4));

        System.out.println(d.removeFirst() + " " + d.pop() + " " + d.remove());
        System.out.println(d);
    }
}

How It Works

  1. Left to right. Java evaluates the operands of + from left to right, so removeFirst() runs first, then pop(), then remove().
  2. Each takes the head. They return 1, 2, and 3 in turn, because each call sees the head left by the previous one.
  3. String building. Integer + " " turns into string concatenation, which produces 1 2 3.
  4. One left. Only 4 remains, so the deque prints [4].

Frequently Asked Questions

It removes and returns the first element (head) of the deque.
removeFirst() throws NoSuchElementException. Use pollFirst() to get null instead.
Yes. For ArrayDeque, pop() and remove() both do exactly what removeFirst() does.
No. ArrayDeque never stores null, and an empty deque throws instead of returning null.
Constant time. The head index simply moves forward; no other elements are shifted.

Next: removeFirstOccurrence()

Remove the first element equal to a value.

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

7 people found this page helpful