The LinkedList.peekLast()
method in Java is used to retrieve, but not remove, the last element of the LinkedList
. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality. Additionally, we will cover a real-world use case to illustrate its application.
Table of Contents
- Introduction
peekLast
Method Syntax- How It Works
- Examples
- Retrieving the Last Element
- Handling an Empty LinkedList
- Real-World Use Case
- Conclusion
Introduction
The LinkedList.peekLast()
method is part of the LinkedList
class in Java. It allows you to access the last element of the list without removing it. This method is particularly useful when you need to inspect the last element without modifying the list.
peekLast Method Syntax
The syntax for the peekLast
method is as follows:
public E peekLast()
- The method does not take any parameters and returns the last element of the
LinkedList
, ornull
if the list is empty.
How It Works
When you use the peekLast()
method, the LinkedList
returns the last element of the list without removing it. If the list is empty, the method returns null
.
Examples
Retrieving the Last Element
The peekLast
method can be used to retrieve the last element of the LinkedList
.
Example
import java.util.LinkedList;
public class PeekLastExample {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
// Retrieve the last element
String lastElement = list.peekLast();
System.out.println("Last element: " + lastElement);
}
}
Output:
Last element: Orange
Handling an Empty LinkedList
If the LinkedList
is empty, the peekLast
method will return null
.
Example
import java.util.LinkedList;
public class PeekLastEmptyListExample {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
// Attempt to retrieve the last element from an empty list
String lastElement = list.peekLast();
if (lastElement == null) {
System.out.println("The list is empty.");
} else {
System.out.println("Last element: " + lastElement);
}
}
}
Output:
The list is empty.
Real-World Use Case
Accessing the Most Recent Task in a Log
In a task management application, you might use a LinkedList
to manage a log of completed tasks. The peekLast()
method can be used to access the most recently completed task without removing it from the log.
Example
import java.util.LinkedList;
class Task {
String description;
Task(String description) {
this.description = description;
}
@Override
public String toString() {
return description;
}
}
public class TaskLog {
public static void main(String[] args) {
LinkedList<Task> taskLog = new LinkedList<>();
taskLog.add(new Task("Write report"));
taskLog.add(new Task("Prepare presentation"));
taskLog.add(new Task("Fix bugs"));
// Peek at the most recent task
Task lastTask = taskLog.peekLast();
System.out.println("Most recently completed task: " + lastTask);
}
}
Output:
Most recently completed task: Fix bugs
Conclusion
The LinkedList.peekLast()
method in Java provides a way to retrieve, but not remove, the last element of a LinkedList
. By understanding how to use this method, you can efficiently inspect the last element of the list without modifying it. This method is particularly useful in real-world applications such as managing a task log or inspecting the last item in a collection without altering the list’s structure.