Java ArrayDeque getLast() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, getLast() returns the tail (last element) of an ArrayDeque without removing it. If the deque is empty, it throws NoSuchElementException.

Its "special value" partner is peekLast(), which returns null on an empty deque. In a queue, the tail is the element that was added most recently.

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, getLast is also declared by the SequencedCollection interface, which Deque extends.

Syntax

Call getLast with no arguments.

Java
Deque<Integer> deque = new ArrayDeque<>(Arrays.asList(10, 20, 30));
int last = deque.getLast();     // 30, deque unchanged
CallWhat it does
deque.getLast()Returns the tail; throws if empty.
deque.peekLast()Returns the tail, or null if empty.
deque.removeLast()Returns the tail and removes it.
deque.getFirst()Reads the other end.

There is no Queue or stack alias for getLast. Reading the tail is a constant-time operation.

Library declarations
Java
// java.util.ArrayDeque<E>
public E getLast()

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

Parameters

ParameterDescription
NonegetLast() takes no parameters.

Return Value

Return ValueDescription
EThe tail of the deque. Never null.

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

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 — Read the Last (and First) Element

Read both ends of a number deque without changing it.

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

public class ArrayDequeGetLastDemo {
    public static void main(String[] args) {
        Deque<Integer> nums = new ArrayDeque<>(Arrays.asList(10, 20, 30));

        System.out.println("Last: " + nums.getLast());
        System.out.println("First: " + nums.getFirst());
        System.out.println("Unchanged: " + nums);
    }
}

How It Works

  1. Tail is 30. The last list element becomes the tail of the deque.
  2. getLast reads it. The call returns 30 and leaves it in place.
  3. The head. getFirst() returns 10 for comparison.
  4. Still three numbers. Neither call removed anything, as the final print shows.

Example 2 — Track the Latest Event

After each event is added at the tail, getLast() shows the most recent one.

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

public class ArrayDequeLatestEvent {
    public static void main(String[] args) {
        Deque<String> log = new ArrayDeque<>();
        String[] events = {"login", "upload", "logout"};

        for (String e : events) {
            log.addLast(e);
            System.out.println("latest = " + log.getLast() + ", count = " + log.size());
        }
    }
}

How It Works

  1. Append each event. addLast(e) puts the newest event at the tail.
  2. Read it right away. getLast() returns the element just added, so it is never called on an empty deque.
  3. Growing log. size() goes from 1 to 3 as events arrive.
  4. History kept. Older events stay in the deque behind the latest one; getLast() only reads the end.

Example 3 — Empty Deque and Single Element

Compare peekLast() and getLast() on an empty deque, then check both ends of a one-element deque.

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

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

        System.out.println("peekLast gives " + d.peekLast());
        try {
            d.getLast();
        } catch (NoSuchElementException e) {
            System.out.println("getLast threw " + e.getClass().getSimpleName());
        }

        d.add("only");
        System.out.println("first == last? " + (d.getFirst() == d.getLast()));
    }
}

How It Works

  1. Safe read. peekLast() on the empty deque returns null.
  2. Throwing read. getLast() on the same empty deque throws NoSuchElementException.
  3. One element. After add("only"), that single element is both the head and the tail.
  4. Same object. == compares references, and both calls return the very same String object, so the result is true.

Frequently Asked Questions

It returns the last element (tail) of the deque without removing it.
getLast() throws NoSuchElementException. peekLast() returns null instead.
getLast() only reads the tail; removeLast() reads and removes it.
The element added most recently with add() or offer(), because those insert at the tail.
Yes. It is a constant-time read on an ArrayDeque.

Next: isEmpty()

Check whether the deque has no elements.

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

9 people found this page helpful