In Java, peek() returns the head of the LinkedList, which is its first element, but leaves it in the list. If the list is empty it returns null and does not throw an exception.
The head means different things for different uses: the next person in a queue, or the top item of a stack that you filled with push. Either way peek lets you look before you decide to take it.
An instance method of java.util.LinkedList (since Java 1.5): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by Queue, and LinkedList implements it. Call it on a LinkedList (or Queue) variable: a plain List variable cannot see it.
Syntax
Call it any time; an empty list is safe.
Java
LinkedList<String> list = new LinkedList<>();
String a = list.peek(); // null, the list is empty
list.add("X");
list.add("Y");
String b = list.peek(); // "X", and the list still has X and Y
Call
What it does
list.peek()
Returns the head, or null if the list is empty.
list.element()
Returns the head, but throws if the list is empty.
list.poll()
Returns the head and removes it.
list.peekLast()
Looks at the tail instead.
peek never changes the list. Call it as often as you like.
Library declarations
Java
// java.util.LinkedList
public E peek()
Parameters
Parameter
Description
None
peek() takes no parameters.
Return Value
Return Value
Description
E
The head of the list, or null if the list is empty.
peek: Blue Sky
peek: Blue Sky
songs: [Blue Sky, Night Drive, Morning Light]
size: 3
How It Works
Three songs. "Blue Sky" is first, so it is the head.
peek returns the head. The first call prints "Blue Sky".
Peeking twice is harmless. The second call returns the same song, because nothing was removed.
All songs remain. The list and the size 3 are exactly as before.
Example 2 — Peek Safely at an Empty List
Write a loop that keeps peeking until the list is empty, and show the null result at the end.
Java
import java.util.LinkedList;
public class LinkedListPeekEmpty {
public static void main(String[] args) {
LinkedList<Integer> stack = new LinkedList<>();
stack.push(10);
stack.push(20);
while (stack.peek() != null) {
int top = stack.peek();
System.out.println("top is " + top);
stack.pop();
}
System.out.println("peek on empty: " + stack.peek());
}
}
Output
top is 20
top is 10
peek on empty: null
How It Works
A stack of two numbers.push puts 20 on top of 10, so 20 is the head.
The loop condition.peek() != null is true while there is still a head.
Look, then pop. Each pass prints the top, 20 first and then 10, and pop() removes it.
Empty at the end. When both are gone, peek() returns null and the loop stops with no exception.
Example 3 — Spot the Difference Between Empty and null
Store a null element on purpose and see why peek cannot tell it apart from an empty list.
Java
import java.util.LinkedList;
public class LinkedListPeekNull {
public static void main(String[] args) {
LinkedList<String> empty = new LinkedList<>();
LinkedList<String> withNull = new LinkedList<>();
withNull.add(null);
System.out.println("empty.peek(): " + empty.peek());
System.out.println("withNull.peek(): " + withNull.peek());
System.out.println("empty.isEmpty(): " + empty.isEmpty());
System.out.println("withNull.isEmpty(): " + withNull.isEmpty());
}
}