Java ArrayDeque offerLast() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, offerLast(e) inserts an element at the tail (end) of an ArrayDeque and returns true. It does the same thing as offer(e) and puts the element in the same place as addLast(e).

It is the tail half of the "special value" insert pair, next to offerFirst. For an ArrayDeque the special value false never appears, because the deque never fills up.

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 offerLast with the element that should become the new tail.

Java
Deque<Integer> deque = new ArrayDeque<>();
deque.offerLast(1);               // [1]
boolean ok = deque.offerLast(2);  // true, [1, 2]
CallWhat it does
deque.offerLast(e)Inserts e at the tail and returns true.
deque.offer(e)Same operation, Queue name.
deque.addLast(e)Same position, returns nothing.
deque.offerLast(null)Throws NullPointerException.

Use offerLast together with offerFirst when your code works at both ends; the matching names make it obvious which end each call touches.

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

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

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 Tail

Offer three numbers to the tail and read the last one.

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

public class ArrayDequeOfferLastDemo {
    public static void main(String[] args) {
        Deque<Integer> d = new ArrayDeque<>();
        d.offerLast(1);
        d.offerLast(2);
        boolean ok = d.offerLast(3);

        System.out.println(ok);
        System.out.println(d + " last=" + d.peekLast());
    }
}

How It Works

  1. Tail after tail. 1, 2, and 3 are added one after another at the end.
  2. Keep the result. Only the third call's return value is stored, and it is true.
  3. Order kept. The deque prints [1, 2, 3], the same order the numbers were offered.
  4. Peek at the tail. peekLast() returns 3 without removing it.

Example 2 — Alternate Between offerFirst and offerLast

Odd numbers go to the tail and even numbers to the head. Print the deque after each step.

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

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

        for (int i = 1; i <= 4; i++) {
            if (i % 2 == 1) {
                d.offerLast(i);
            } else {
                d.offerFirst(i);
            }
            System.out.println(d);
        }
    }
}

How It Works

  1. i = 1 is odd. offerLast(1) on an empty deque gives [1].
  2. i = 2 is even. offerFirst(2) places 2 in front: [2, 1].
  3. i = 3 goes last. 3 joins the tail, giving [2, 1, 3].
  4. i = 4 goes first. 4 becomes the new head, so the final deque is [4, 2, 1, 3].

Example 3 — Fill an Inbox, Then Read It in Order

Messages are offered to the tail, then read from the head with a pollFirst() loop that stops at null.

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

public class ArrayDequeInbox {
    public static void main(String[] args) {
        Deque<String> inbox = new ArrayDeque<>();
        String[] incoming = {"hi", "meeting at 3", "lunch?"};

        for (String m : incoming) {
            inbox.offerLast(m);
        }

        int n = 1;
        String msg;
        while ((msg = inbox.pollFirst()) != null) {
            System.out.println(n + ") " + msg);
            n++;
        }
    }
}

How It Works

  1. Three messages arrive. The loop offers each one to the tail, so the deque matches the array order.
  2. Read and remove. (msg = inbox.pollFirst()) != null takes the head and checks it in one expression.
  3. null means done. Because an ArrayDeque can never contain null, a null from pollFirst() can only mean the inbox is empty.
  4. Numbered output. The counter n numbers the messages 1 to 3 in arrival order.

Frequently Asked Questions

It inserts the element at the tail (end) of the deque and returns true.
Yes. For ArrayDeque, offer(e) and offerLast(e) do the same thing.
Both insert at the tail. offerLast returns true; addLast returns void.
Only with an exception: NullPointerException for a null element. It never returns false.
Take them from the head with pollFirst() (or poll()). That gives first-in, first-out order.

Next: peek()

Read the head without removing it, or get null.

peek() 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