Java ArrayDeque offer() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, offer(e) inserts an element at the tail of an ArrayDeque and returns true. It is the queue-style insert from the Queue interface and does the same thing as offerLast(e).

In queue code it pairs with poll() to take elements from the head and peek() to look at the head, giving first-in, first-out (FIFO) order.

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 offer on a Queue or Deque variable.

Java
Queue<String> queue = new ArrayDeque<>();
boolean ok = queue.offer("job1");   // true, queue = [job1]
queue.offer("job2");                // queue = [job1, job2]
CallWhat it does
queue.offer(e)Inserts e at the tail and returns true.
queue.add(e)Same result for ArrayDeque.
deque.offerLast(e)The Deque name for the same operation.
queue.offer(null)Throws NullPointerException. It does not return false.

The difference between offer and add only shows on queues with a size limit: there, offer returns false when the queue is full and add throws IllegalStateException. An ArrayDeque is never full, so both always succeed.

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

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

Parameters

ParameterDescription
eRequired. The element to insert at the tail. 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 offer().

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 — A Printer Queue with offer and poll

Documents are offered to the tail and printed from the head in arrival order.

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

public class ArrayDequeOfferDemo {
    public static void main(String[] args) {
        Queue<String> printer = new ArrayDeque<>();
        System.out.println(printer.offer("doc1"));
        printer.offer("doc2");
        printer.offer("doc3");

        System.out.println(printer);
        System.out.println("printing " + printer.poll());
        System.out.println(printer);
    }
}

How It Works

  1. Queue interface. Declaring printer as Queue<String> limits it to queue methods such as offer, poll, and peek.
  2. offer reports true. The first call is printed directly and shows true.
  3. FIFO order. Three documents in, and the deque prints them in the order they were offered.
  4. Take from the head. poll() removes doc1, the oldest document, leaving [doc2, doc3].

Example 2 — The Constructor Number Is Not a Limit

Create a deque with an initial size of 2, then offer three elements. Every offer still succeeds.

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

public class ArrayDequeOfferNoLimit {
    public static void main(String[] args) {
        Deque<Integer> d = new ArrayDeque<>(2);

        boolean a = d.offer(1);
        boolean b = d.offer(2);
        boolean c = d.offer(3);

        System.out.println(a + " " + b + " " + c);
        System.out.println(d + " size=" + d.size());
    }
}

How It Works

  1. A starting size. new ArrayDeque<>(2) only tells the deque how many elements to expect at first. It is not a maximum.
  2. Growing past the hint. The third offer makes the deque grow its internal array automatically.
  3. All true. Since nothing can be full, each offer returns true.
  4. Where offer differs. A bounded queue such as ArrayBlockingQueue created with capacity 2 would return false for the third offer.

Example 3 — Round-Robin Turns

Each player takes a turn from the head and is offered back to the tail, so turns rotate fairly.

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

public class ArrayDequeRoundRobin {
    public static void main(String[] args) {
        Queue<String> players = new ArrayDeque<>(Arrays.asList("Ann", "Raj", "Li"));

        for (int turn = 1; turn <= 5; turn++) {
            String p = players.poll();
            System.out.println("Turn " + turn + ": " + p);
            players.offer(p);
        }
        System.out.println(players);
    }
}

How It Works

  1. Three players. The constructor copies the list, so the head is Ann and the tail is Li.
  2. Take a turn. Each pass polls the head player and prints the turn number.
  3. Back of the line. offer(p) sends the same player to the tail, so after Li comes Ann again.
  4. Final order. After five turns Ann and Raj have each played twice and Li once, and Li is now at the head: [Li, Ann, Raj].

Frequently Asked Questions

It inserts the element at the tail of the deque and returns true.
On capacity-limited queues, offer() returns false when full while add() throws. ArrayDeque has no limit, so both always succeed and return true.
Yes. For ArrayDeque, offer(e) does exactly what offerLast(e) does.
No. It throws NullPointerException, because ArrayDeque never holds null.
poll() removes and returns the head (or null when empty), and peek() reads the head without removing it.

Next: offerFirst()

Insert an element at the head and return true.

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

12 people found this page helpful