Java LinkedList peekFirst() Method
Example
Get the first item in a list:
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<String> cars = new LinkedList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
System.out.println(cars.peekFirst());
System.out.println(cars);
}
}
Definition and Usage
The peekFirst() method returns the first item in the list. It does not remove the item.
It works like peek(). If the list is empty, it returns null.
Tip: Use the peekLast() method to get the last item in the list.
Syntax
public T peekFirst()
T refers to the data type of items in the list.
Parameter Values
None.
Technical Details
| Returns: | The first item in the list, or null if the list is empty. |
|---|
Related Pages
❮ LinkedList Methods