Java ArrayDeque peek() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, peek() returns the head of an ArrayDeque without removing it. If the deque is empty, it returns null.

It is the same as peekFirst(), and it works for both roles of a deque: in a queue it shows the next element poll() would return, and in a stack it shows the top that pop() would remove.

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

Java
Deque<Integer> stack = new ArrayDeque<>();
stack.push(10);
stack.push(20);
Integer top = stack.peek();     // 20, nothing removed
CallWhat it does
deque.peek()Returns the head, or null if empty.
deque.element()Returns the head; throws if empty.
deque.peekFirst()Same as peek().
deque.peekLast()Returns the tail instead.

peek() is declared in both Queue and Deque, so it is available whichever interface type you use for the variable.

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

// Declared in Queue<E> and Deque<E>

Parameters

ParameterDescription
Nonepeek() takes no parameters.

Return Value

Return ValueDescription
EThe head 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 peek().

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 — Peek at the Front of a Queue

Peeking twice returns the same element and leaves the queue size unchanged.

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

public class ArrayDequePeekDemo {
    public static void main(String[] args) {
        Queue<String> lights = new ArrayDeque<>(Arrays.asList("red", "amber", "green"));

        System.out.println("peek: " + lights.peek());
        System.out.println("peek again: " + lights.peek());
        System.out.println("size: " + lights.size());
    }
}

How It Works

  1. Front of the queue. red came first in the list, so it is the head.
  2. Look, don't take. peek() returns red but does not remove it.
  3. Repeatable. A second peek() gives the same answer.
  4. Nothing removed. size() is still 3.

Example 2 — Peek at the Top of a Stack

Watch the top change as elements are popped, until peek() returns null.

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

public class ArrayDequePeekStack {
    public static void main(String[] args) {
        Deque<Integer> stack = new ArrayDeque<>();
        stack.push(10);
        stack.push(20);

        System.out.println("top: " + stack.peek());
        stack.pop();
        System.out.println("top: " + stack.peek());
        stack.pop();
        System.out.println("top: " + stack.peek());
    }
}

How It Works

  1. Top after two pushes. 20 was pushed last, so peek() returns 20.
  2. Pop changes the top. After one pop(), the top is 10.
  3. Empty stack. After the second pop(), peek() returns null.
  4. No exception. Unlike Stack.peek(), which throws EmptyStackException, ArrayDeque.peek() just returns null.

Example 3 — Remove Items While peek() Says So

Deadlines are kept in time order. While the head is due, poll it; stop at the first one that is still in the future.

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

public class ArrayDequeExpire {
    public static void main(String[] args) {
        Deque<Integer> deadlines = new ArrayDeque<>(Arrays.asList(2, 5, 9, 12));
        int now = 6;

        while (deadlines.peek() != null && deadlines.peek() <= now) {
            System.out.println("expired: " + deadlines.poll());
        }
        System.out.println("left: " + deadlines);
    }
}

How It Works

  1. Sorted times. The earliest deadline is at the head, so only the head ever needs to be checked.
  2. null check first. deadlines.peek() != null comes before the comparison, and && stops early if the deque is empty.
  3. Due items leave. 2 and 5 are not later than now (6), so each is polled and printed.
  4. Stop at the future. The head is now 9, which is greater than 6, so the loop ends and [9, 12] remain.

Frequently Asked Questions

It returns the head of the deque without removing it, or null if the deque is empty.
Both read the head. peek() returns null on an empty deque; element() throws NoSuchElementException.
Yes. push() inserts at the head and peek() reads the head, so peek() returns the most recently pushed element.
Yes. For ArrayDeque, peek() does exactly what peekFirst() does.
No. That exception belongs to the legacy Stack class. ArrayDeque.peek() returns null when empty.

Next: peekFirst()

Read the first element, or get null if empty.

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

15 people found this page helpful