Home Java LinkedList Methods peekFirst() Java LinkedList peekFirst() Method 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.
Deque<String> deque = new LinkedList<>();
String x = deque.peekFirst(); // null, empty
deque.addLast("A");
deque.addLast("B");
String y = deque.peekFirst(); // "A" Call What 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.util.LinkedList
public E peekFirst() Parameters Parameter Description None peekFirst() takes no parameters.
Return Value Return Value Description EThe first element, or null if the list is empty.
Compare with Similar Methods Methods that look at the ends:
Method How 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 1. Look at the front 2. Empty deque 3. Peek while processing Example 1 — Read the Front of a Deque Peek at the front of a deque of cards and check that the deque is unchanged.
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);
}
} front card: Queen
back card: Ace
deck: [Queen, King, Ace] How It Works Cards added at the back. "Queen" went in first, so it is at the front of the deque.peekFirst reads the front. It returns "Queen" without taking it out.peekLast reads the back. It returns "Ace", the most recently added card.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.
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));
}
} peekFirst: null
getFirst -> NoSuchElementException
is null: true How It Works The deque has nothing in it. There is no first element.peekFirst answers null. The variable a becomes null, and the program carries on.getFirst answers with an exception. It throws NoSuchElementException, which the catch block turns into a message.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.
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);
}
} too young: 4
too young: 9
too young: 17
first adult: 30
left: [30] How It Works Sorted ages. The deque holds 4, 9, 17 and 30, from youngest to oldest.Peek decides. The loop only continues while the first age exists and is under 18.pollFirst removes the young ones. 4, 9 and 17 are each reported and taken out of the front.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 What does LinkedList peekFirst() do? It returns the first element without removing it, or null if the list is empty.
What is the difference between peekFirst() and getFirst()? On an empty list peekFirst returns null, and getFirst throws NoSuchElementException.
Is peekFirst() the same as peek()? Yes. peek comes from the Queue interface and peekFirst from Deque. Both look at the first element.
Does peekFirst() remove the element? No. To remove it too, use pollFirst() or removeFirst().
Can peekFirst() return null for a non-empty list? Yes, if the first element itself is null. Use isEmpty() to tell the two cases apart.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion