Java ArrayDeque offerFirst() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, offerFirst(e) inserts an element at the head (front) of an ArrayDeque and returns true. It puts the element in the same place as addFirst(e) and push(e).

It belongs to the "special value" family of Deque methods. On a deque with a size limit it would return false when full. An ArrayDeque is never full, so it always 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.

Syntax

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

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

Choosing between offerFirst and addFirst on an ArrayDeque is mostly a matter of style. Code written against the Deque interface often prefers offerFirst so it also behaves well with capacity-limited deques.

Library declarations
Java
// java.util.ArrayDeque<E>
public boolean offerFirst(E e)

// Declared in Deque<E>

Parameters

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

Return Value

Return ValueDescription
booleanAlways true for an ArrayDeque.

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

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 — Insert at the Head and Check the Result

Put "a" in front of an existing deque and print the returned value.

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

public class ArrayDequeOfferFirstDemo {
    public static void main(String[] args) {
        Deque<String> d = new ArrayDeque<>(Arrays.asList("b", "c"));

        boolean ok = d.offerFirst("a");

        System.out.println(ok + " " + d);
        System.out.println("head = " + d.peekFirst());
    }
}

How It Works

  1. Existing elements. The deque starts as [b, c].
  2. New head. offerFirst("a") inserts a before b.
  3. Boolean result. ok is true, printed together with the updated deque [a, b, c].
  4. Confirm the head. peekFirst() returns a, the element just offered.

Example 2 — offerFirst(null) Throws Instead of Returning false

Even the "special value" insert refuses null with an exception.

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

public class ArrayDequeOfferFirstNull {
    public static void main(String[] args) {
        Deque<String> d = new ArrayDeque<>();
        d.offerFirst("ok");

        try {
            boolean result = d.offerFirst(null);
            System.out.println("returned " + result);
        } catch (NullPointerException e) {
            System.out.println("offerFirst(null): " + e.getClass().getSimpleName());
        }
        System.out.println("size = " + d.size());
    }
}

How It Works

  1. A normal offer. "ok" becomes the only element.
  2. The print is skipped. offerFirst(null) throws before it can return, so "returned ..." is never printed.
  3. Exception, not false. The catch block shows that a NullPointerException was thrown.
  4. Unchanged deque. The size is still 1, because the null was never stored.

Example 3 — Put a Failed Task Back at the Front

Take a task from the head; if it fails, offer it back to the head so it is retried before the others.

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

public class ArrayDequeRetryTask {
    public static void main(String[] args) {
        Deque<String> tasks = new ArrayDeque<>(Arrays.asList("upload", "resize", "notify"));

        String task = tasks.pollFirst();
        boolean failed = task.equals("upload");
        if (failed) {
            tasks.offerFirst(task);
        }

        System.out.println("After retry: " + tasks);
        System.out.println("Running " + tasks.pollFirst() + " again");
        System.out.println("Waiting: " + tasks);
    }
}

How It Works

  1. Take the first task. pollFirst() removes "upload", leaving [resize, notify].
  2. Simulated failure. The program treats "upload" as failed, so failed is true.
  3. Back to the front. offerFirst(task) returns the task to the head, so the deque looks just as it did before.
  4. Retried next. The next pollFirst() gets upload again, and the other two tasks keep waiting in order.

Frequently Asked Questions

It inserts the element at the head (front) of the deque and returns true.
No. ArrayDeque grows as needed, so offerFirst always returns true.
Same position. offerFirst returns a boolean, addFirst returns void. They differ in behavior only on capacity-limited deques.
It throws NullPointerException. ArrayDeque never stores null.
Both insert at the head. push() returns void and is the stack-style name; offerFirst() returns true.

Next: offerLast()

Insert an element at the tail and return true.

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

13 people found this page helpful