Java ArrayDeque peekFirst() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

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

It is the "special value" version of getFirst(), and peek() does exactly the same. Its mirror at the tail is peekLast().

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

Java
Deque<String> deque = new ArrayDeque<>();
String head = deque.peekFirst();   // null (empty)
deque.add("x");
head = deque.peekFirst();          // "x"
CallWhat it does
deque.peekFirst()Returns the head, or null.
deque.getFirst()Returns the head; throws if empty.
deque.peek()Same as peekFirst().
deque.pollFirst()Returns the head and removes it.

Because an ArrayDeque cannot hold null, a null from peekFirst() is a reliable "empty" signal.

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

// Declared in Deque<E>

Parameters

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

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 Head After Mixed Inserts

Add at both ends, then peek at the head.

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

public class ArrayDequePeekFirstDemo {
    public static void main(String[] args) {
        Deque<String> d = new ArrayDeque<>();
        d.addLast("x");
        d.addLast("y");
        d.addFirst("w");

        System.out.println(d.peekFirst());
        System.out.println(d);
    }
}

How It Works

  1. Tail inserts. x and then y are added at the tail.
  2. Head insert. addFirst("w") makes w the new head.
  3. Peek. peekFirst() returns w.
  4. Unchanged. The deque still prints all three elements, [w, x, y].

Example 2 — Show a Default Text When Empty

Use the null result to print a placeholder.

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

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

        String head = d.peekFirst();
        System.out.println(head == null ? "(empty)" : head);

        d.add("ready");
        head = d.peekFirst();
        System.out.println(head == null ? "(empty)" : head);
    }
}

How It Works

  1. Nothing to read. On the empty deque, peekFirst() returns null.
  2. Placeholder. The conditional operator prints (empty) when head is null.
  3. After adding. "ready" becomes the head, so the second peek returns it.
  4. No try/catch needed. With getFirst(), the first call would have thrown instead.

Example 3 — Merge Two Sorted Deques

Compare the two heads with peekFirst() and always take the smaller one.

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

public class ArrayDequeMergeSorted {
    public static void main(String[] args) {
        Deque<Integer> a = new ArrayDeque<>(Arrays.asList(1, 4, 7));
        Deque<Integer> b = new ArrayDeque<>(Arrays.asList(2, 3, 8));
        List<Integer> merged = new ArrayList<>();

        while (!a.isEmpty() && !b.isEmpty()) {
            if (a.peekFirst() <= b.peekFirst()) {
                merged.add(a.pollFirst());
            } else {
                merged.add(b.pollFirst());
            }
        }
        merged.addAll(a);
        merged.addAll(b);
        System.out.println(merged);
    }
}

How It Works

  1. Two sorted inputs. Each deque's smallest value is at its head.
  2. Compare heads. peekFirst() reads both heads without removing them; <= unboxes and compares the numbers.
  3. Take the smaller. Only the winning head is removed with pollFirst(). The order taken is 1, 2, 3, 4, 7.
  4. Leftovers. When a runs out, b still holds 8. The two addAll calls append whatever is left, giving [1, 2, 3, 4, 7, 8].

Frequently Asked Questions

It returns the first element (head) without removing it, or null if the deque is empty.
Both read the head. peekFirst() returns null on an empty deque; getFirst() throws NoSuchElementException.
Yes. peek() is the Queue name for the same operation.
No. Use pollFirst() or removeFirst() to remove it.
Yes for ArrayDeque, because it can never contain null elements.

Next: peekLast()

Read the last element, or get null if empty.

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

7 people found this page helpful