Java LinkedList peekLast() Method
Example
Get the last 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.peekLast());
System.out.println(cars);
}
}
Definition and Usage
The peekLast() method returns the last item in the list. It does not remove the item.
If the list is empty, it returns null. The getLast() method does the same, but it throws an exception when the list is empty.
Tip: Use the peekFirst() method to get the first item in the list.
Syntax
public T peekLast()
T refers to the data type of items in the list.
Parameter Values
None.
Technical Details
| Returns: | The last item in the list, or null if the list is empty. |
|---|
Related Pages
❮ LinkedList Methods