Java ArrayDeque peekLast() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

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

It is the "special value" version of getLast(). Its mirror at the head is peekFirst().

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 peekLast with no arguments.

Java
Deque<String> deque = new ArrayDeque<>(Arrays.asList("mon", "tue"));
String last = deque.peekLast();    // "tue", deque unchanged
CallWhat it does
deque.peekLast()Returns the tail, or null.
deque.getLast()Returns the tail; throws if empty.
deque.pollLast()Returns the tail and removes it.
deque.peekFirst()Reads the head instead.

There is no Queue or stack alias for peekLast. It is handy whenever you need to compare a new element with the one added just before it.

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

// Declared in Deque<E>

Parameters

ParameterDescription
NonepeekLast() takes no parameters.

Return Value

Return ValueDescription
EThe tail of the deque, left in place.
nullIf the deque is empty.

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

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 Element

Peek at the tail of a list of days.

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

public class ArrayDequePeekLastDemo {
    public static void main(String[] args) {
        Deque<String> days = new ArrayDeque<>(Arrays.asList("mon", "tue", "wed"));

        System.out.println("peekLast: " + days.peekLast());
        System.out.println("deque: " + days);
    }
}

How It Works

  1. Three days. wed is the last element in the list, so it is the tail.
  2. Read the tail. peekLast() returns wed.
  3. Nothing removed. The deque still prints all three days.
  4. When to switch. To remove wed instead of just reading it, call pollLast().

Example 2 — Skip Consecutive Duplicates

Only add a value if it differs from the current tail, so runs of the same value collapse into one.

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

public class ArrayDequeCompact {
    public static void main(String[] args) {
        String[] inputs = {"a", "a", "b", "b", "b", "a"};
        Deque<String> compact = new ArrayDeque<>();

        for (String s : inputs) {
            if (!s.equals(compact.peekLast())) {
                compact.addLast(s);
            }
        }
        System.out.println(compact);
    }
}

How It Works

  1. First value. The deque is empty, so peekLast() is null; "a".equals(null) is false, and a is added.
  2. Repeats are skipped. The second a equals the tail, so it is not added. The same happens to the extra b values.
  3. A change is kept. The final a differs from the tail b, so it is added again.
  4. Result. Runs collapse, giving [a, b, a]. This is not the same as removing all duplicates.

Example 3 — peekLast While Removing from the Tail

Peek after each pollLast() until the deque is empty.

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

public class ArrayDequePeekLastShrink {
    public static void main(String[] args) {
        Deque<Integer> d = new ArrayDeque<>(Arrays.asList(7, 8));

        System.out.println(d.peekLast());
        d.pollLast();
        System.out.println(d.peekLast());
        d.pollLast();
        System.out.println(d.peekLast());
    }
}

How It Works

  1. Start. The tail is 8.
  2. Remove it. After one pollLast(), 7 is both the head and the tail.
  3. Remove again. The second pollLast() empties the deque.
  4. Empty tail. The last peekLast() returns null instead of throwing.

Frequently Asked Questions

It returns the last element (tail) without removing it, or null if the deque is empty.
Both read the tail. peekLast() returns null on an empty deque; getLast() throws NoSuchElementException.
No. Use pollLast() or removeLast() to remove it.
No. Queue methods work at the head for reading; peekLast() is a Deque method.
Call equals on the new value: newValue.equals(deque.peekLast()). It returns false when the deque is empty.

Next: poll()

Remove and return the head, or null if empty.

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

8 people found this page helpful