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]
Call
What 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
Parameter
Description
e
Required. The element to insert at the tail. Must not be null.
Return Value
Return Value
Description
boolean
Always 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 returnsnull or false instead. The highlighted cell matches offerLast().
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());
}
}
Output
true
[1, 2, 3] last=3
How It Works
Tail after tail. 1, 2, and 3 are added one after another at the end.
Keep the result. Only the third call's return value is stored, and it is true.
Order kept. The deque prints [1, 2, 3], the same order the numbers were offered.
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);
}
}
}
Output
[1]
[2, 1]
[2, 1, 3]
[4, 2, 1, 3]
How It Works
i = 1 is odd.offerLast(1) on an empty deque gives [1].
i = 2 is even.offerFirst(2) places 2 in front: [2, 1].
i = 3 goes last. 3 joins the tail, giving [2, 1, 3].
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++;
}
}
}
Output
1) hi
2) meeting at 3
3) lunch?
How It Works
Three messages arrive. The loop offers each one to the tail, so the deque matches the array order.
Read and remove.(msg = inbox.pollFirst()) != null takes the head and checks it in one expression.
null means done. Because an ArrayDeque can never contain null, a null from pollFirst() can only mean the inbox is empty.
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.