Java ArrayDeque addFirst() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, addFirst(e) inserts an element at the head (the front) of an ArrayDeque. The elements that were already there now come after it, in the same order. The method returns void.

It is the same operation as push(e), which is the name you use when the deque acts as a stack. offerFirst(e) also inserts at the head but returns true.

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

Syntax

Call addFirst with the element that should become the new head.

Java
Deque<String> deque = new ArrayDeque<>();
deque.addFirst("b");      // [b]
deque.addFirst("a");      // [a, b]
CallWhat it does
deque.addFirst(e)Inserts e at the head. Returns nothing.
deque.push(e)Exactly the same; stack-style name.
deque.offerFirst(e)Same position, returns true.
deque.addFirst(null)Throws NullPointerException.

Because addFirst returns void, code like boolean ok = deque.addFirst(x); does not compile. Use offerFirst if you want a boolean.

Library declarations
Java
// java.util.ArrayDeque<E>
public void addFirst(E e)

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

Parameters

ParameterDescription
eRequired. The element to insert at the head. Must not be null.

Return Value

Return ValueDescription
voidNothing is returned. The deque has one more element, at the head.

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

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 — Build a Deque from the Front

Insert C, then B, then A at the head and see the final order.

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

public class ArrayDequeAddFirstDemo {
    public static void main(String[] args) {
        Deque<String> deque = new ArrayDeque<>();
        deque.addFirst("C");
        deque.addFirst("B");
        deque.addFirst("A");

        System.out.println(deque);
        System.out.println("First: " + deque.getFirst());
        System.out.println("Last: " + deque.getLast());
    }
}

How It Works

  1. The first insert. With an empty deque, C is both the head and the tail.
  2. Each new head. B goes in front of C, and then A goes in front of B.
  3. Reading the result. The deque prints [A, B, C]: the last element inserted with addFirst is shown first.
  4. The ends. getFirst() returns A and getLast() returns C, the very first element that went in.

Example 2 — Let an Urgent Job Jump the Queue

Normal jobs join at the tail, but an urgent job is placed at the head so it runs next.

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

public class ArrayDequeUrgentJob {
    public static void main(String[] args) {
        Deque<String> jobs = new ArrayDeque<>();
        jobs.addLast("print report");
        jobs.addLast("send email");
        jobs.addFirst("fix outage");

        while (!jobs.isEmpty()) {
            System.out.println("Doing: " + jobs.pollFirst());
        }
    }
}

How It Works

  1. Normal order. Two jobs are added at the tail, giving [print report, send email].
  2. Priority insert. addFirst("fix outage") places the urgent job ahead of both.
  3. Run from the head. The loop takes jobs with pollFirst() until isEmpty() is true.
  4. Result. The urgent job runs first, and the other two keep their original order.

Example 3 — Reverse an Array with addFirst

Adding every value at the head reverses the order without any index math.

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

public class ArrayDequeReverseArray {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4};
        Deque<Integer> reversed = new ArrayDeque<>();

        for (int n : nums) {
            reversed.addFirst(n);
        }

        System.out.println(reversed);
        System.out.println("Size: " + reversed.size());
    }
}

How It Works

  1. Walk the array forward. The enhanced for loop visits 1, 2, 3, and 4 in that order.
  2. Each value becomes the head. 1 is inserted first, then 2 goes in front of it, and so on, each value boxed to an Integer.
  3. Reversed output. The last value visited, 4, ends up at the head, so the deque prints [4, 3, 2, 1].
  4. Nothing lost. All four values are stored, so size() is 4.

Frequently Asked Questions

It inserts the element at the head (front) of the deque. It returns nothing.
Yes. For ArrayDeque, push(e) simply calls addFirst(e). Use push when you think of the deque as a stack.
Both insert at the head. addFirst returns void; offerFirst returns true. On an ArrayDeque neither can fail for lack of space.
No. addFirst(null) throws NullPointerException.
It runs in amortized constant time: the deque occasionally grows its internal array, but no elements are shifted on each insert.

Next: addLast()

Insert an element at the tail (end) of the deque.

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

9 people found this page helpful