Java LinkedList peekFirst() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, peekFirst() returns the first element of the LinkedList but leaves it in place. If the list is empty it returns null.

It does the same as peek(). The Deque interface gives it a name that matches peekLast(), so code that works with both ends reads clearly.

An instance method of java.util.LinkedList (since Java 1.6): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by Deque, and LinkedList implements it. Call it on a LinkedList (or Deque) variable: a plain List variable cannot see it.

Syntax

Use it on a Deque or LinkedList variable.

Java
Deque<String> deque = new LinkedList<>();
String x = deque.peekFirst();     // null, empty
deque.addLast("A");
deque.addLast("B");
String y = deque.peekFirst();     // "A"
CallWhat it does
deque.peekFirst()Returns the first element, or null if empty.
deque.peekLast()Returns the last element, or null if empty.
deque.getFirst()Returns the first element, but throws if empty.
deque.pollFirst()Returns the first element and removes it.

peekFirst never changes the list.

Library declarations
Java
// java.util.LinkedList
public E peekFirst()

Parameters

ParameterDescription
NonepeekFirst() takes no parameters.

Return Value

Return ValueDescription
EThe first element, or null if the list is empty.

Compare with Similar Methods

Methods that look at the ends:

MethodHow it differs
peek()The Queue name for the same operation.
getFirst()Same element, but throws NoSuchElementException when empty.
peekLast()Looks at the other end.
pollFirst()Looks at the first element and also removes it.

Java Examples

Example 1 — Read the Front of a Deque

Peek at the front of a deque of cards and check that the deque is unchanged.

Java
import java.util.Deque;
import java.util.LinkedList;

public class LinkedListPeekFirstDemo {
    public static void main(String[] args) {
        Deque<String> cards = new LinkedList<>();
        cards.addLast("Queen");
        cards.addLast("King");
        cards.addLast("Ace");

        System.out.println("front card: " + cards.peekFirst());
        System.out.println("back card:  " + cards.peekLast());
        System.out.println("deck: " + cards);
    }
}

How It Works

  1. Cards added at the back. "Queen" went in first, so it is at the front of the deque.
  2. peekFirst reads the front. It returns "Queen" without taking it out.
  3. peekLast reads the back. It returns "Ace", the most recently added card.
  4. The deck is intact. Nothing was removed, so all three cards are printed.

Example 2 — Peek at an Empty Deque and Compare with getFirst

See that peekFirst gives null and getFirst throws on an empty deque.

Java
import java.util.LinkedList;
import java.util.NoSuchElementException;

public class LinkedListPeekFirstEmpty {
    public static void main(String[] args) {
        LinkedList<String> deque = new LinkedList<>();

        String a = deque.peekFirst();
        System.out.println("peekFirst: " + a);

        try {
            deque.getFirst();
        } catch (NoSuchElementException e) {
            System.out.println("getFirst -> NoSuchElementException");
        }

        System.out.println("is null: " + (a == null));
    }
}

How It Works

  1. The deque has nothing in it. There is no first element.
  2. peekFirst answers null. The variable a becomes null, and the program carries on.
  3. getFirst answers with an exception. It throws NoSuchElementException, which the catch block turns into a message.
  4. A normal null test works. a == null is true, which is how callers usually react to peekFirst.

Example 3 — Peek Before Deciding What to Do

Look at the first number in a deque and drop it only when it is too small.

Java
import java.util.Deque;
import java.util.LinkedList;

public class LinkedListPeekFirstScan {
    public static void main(String[] args) {
        Deque<Integer> ages = new LinkedList<>();
        ages.add(4);
        ages.add(9);
        ages.add(17);
        ages.add(30);

        while (ages.peekFirst() != null && ages.peekFirst() < 18) {
            System.out.println("too young: " + ages.pollFirst());
        }

        System.out.println("first adult: " + ages.peekFirst());
        System.out.println("left: " + ages);
    }
}

How It Works

  1. Sorted ages. The deque holds 4, 9, 17 and 30, from youngest to oldest.
  2. Peek decides. The loop only continues while the first age exists and is under 18.
  3. pollFirst removes the young ones. 4, 9 and 17 are each reported and taken out of the front.
  4. It stops at 30. That age is 18 or more, so it stays, and peekFirst() now shows it as the first adult.

Frequently Asked Questions

It returns the first element without removing it, or null if the list is empty.
On an empty list peekFirst returns null, and getFirst throws NoSuchElementException.
Yes. peek comes from the Queue interface and peekFirst from Deque. Both look at the first element.
No. To remove it too, use pollFirst() or removeFirst().
Yes, if the first element itself is null. Use isEmpty() to tell the two cases apart.

Next: peekLast()

Look at the last element without removing it; 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.

11 people found this page helpful