Java ArrayDeque addLast() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, addLast(e) inserts an element at the tail (the end) of an ArrayDeque. The elements already in the deque stay where they are. The method returns void.

It is the same operation as add(e), except that add returns true. offerLast(e) also inserts at the tail and 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, addLast is also declared by the SequencedCollection interface, which Deque extends.

Syntax

Call addLast with the element that should become the new tail.

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

addLast pairs naturally with removeFirst or pollFirst for a first-in, first-out queue, and with pollLast for a stack that grows at the tail.

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

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

Parameters

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

Return Value

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

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

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 Simple Ticket Queue

Tickets are added at the tail and served from the head.

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

public class ArrayDequeAddLastDemo {
    public static void main(String[] args) {
        Deque<Integer> tickets = new ArrayDeque<>();
        tickets.addLast(101);
        tickets.addLast(102);
        tickets.addLast(103);

        System.out.println(tickets);
        System.out.println("Last: " + tickets.getLast());
        System.out.println("Next served: " + tickets.removeFirst());
        System.out.println(tickets);
    }
}

How It Works

  1. Arrival order. Tickets 101, 102, and 103 are each placed behind the previous one.
  2. The newest ticket. getLast() reads the tail, which is 103, without removing it.
  3. Serve the oldest. removeFirst() takes 101 off the head and returns it.
  4. What remains. The deque prints [102, 103], ready for the next customer.

Example 2 — Mix addFirst, addLast, and add

Grow the deque from both sides and check where add() puts its element.

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

public class ArrayDequeAddLastMix {
    public static void main(String[] args) {
        Deque<String> d = new ArrayDeque<>();
        d.addLast("middle");
        d.addFirst("left");
        d.addLast("right");
        System.out.println(d);

        d.add("far right");
        System.out.println(d);
    }
}

How It Works

  1. A starting point. "middle" is the only element after the first call.
  2. Grow left and right. addFirst("left") goes before it and addLast("right") after it.
  3. add is addLast. d.add("far right") puts its element at the tail too, behind "right".
  4. Head to tail. println lists elements from head to tail, so the left-to-right picture matches the method names.

Example 3 — Keep the Last Three Readings

A sliding window: add each new reading at the tail and drop the oldest from the head when there are more than three.

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

public class ArrayDequeSlidingWindow {
    public static void main(String[] args) {
        int[] readings = {4, 8, 15, 16, 23};
        Deque<Integer> window = new ArrayDeque<>();

        for (int r : readings) {
            window.addLast(r);
            if (window.size() > 3) {
                window.removeFirst();
            }
            System.out.println(window);
        }
    }
}

How It Works

  1. Fill up. The first three readings simply go to the tail, so the window grows to [4, 8, 15].
  2. Too many. Adding 16 makes four elements, so removeFirst() drops 4, the oldest reading.
  3. Slide again. Adding 23 pushes the size to four once more, and 8 is dropped from the head.
  4. Cheap at both ends. Adding at the tail and removing at the head are both constant-time for an ArrayDeque, which makes it a good fit for windows like this.

Frequently Asked Questions

It inserts the element at the tail (end) of the deque and returns nothing.
They put the element in the same place. add() returns true, while addLast() returns void.
Both insert at the tail. offerLast returns true; addLast returns void. For ArrayDeque neither can fail for lack of space.
No. addLast(null) throws NullPointerException.
removeLast() or pollLast() take it back from the tail. removeFirst() or pollFirst() take the oldest element from the head instead.

Next: clear()

Remove every element at once.

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

10 people found this page helpful