Java LinkedList peekLast() Method
peekLast()is a [method] in JavaLinkedListmethod provided by the class, used to view but not remove the last element of the linked list. If the linked list is empty, this method returnsnull。
Syntax Format
E peekLast()
Method Characteristics
1. Non-destructive operation
peekLast()The method only views the last element of the linked list and does not remove that element from the linked list.
2. Empty linked list handling
When the linked list is empty,peekLast()it returnsnulland does not throw an exception.
3. Time complexity
SinceLinkedListis a doubly linked list implementation,peekLast()the time complexity of the method is O(1).
Method Parameters and Return Value
Parameters
This method does not accept any parameters.
Return Value
- Returns the last element of the linked list.
- If the linked list is empty, it returns
null
Usage Examples
Basic Usage
Example
public class PeekLastExample {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
String lastElement = list.peekLast();
System.out.println("Last element: " + lastElement); // Output: Cherry
System.out.println("LinkedList after peek: " + list); // The linked list remains unchanged
}
}
Empty Linked List Case
Example
public class EmptyListExample {
public static void main(String[] args) {
LinkedList<String> emptyList = new LinkedList<>();
String result = emptyList.peekLast();
System.out.println("Result from empty list: " + result); // Output: null
}
}
Comparison of Related Methods
| Method Name | Function Description | Behavior When the Linked List Is Empty | Whether the Linked List Is Modified |
|---|---|---|---|
peekLast() |
View the last element | Returns null | no |
getLast() |
Get the last element | Throws an exception | no |
removeLast() |
Remove and return the last element | Throws an exception | Yes |
pollLast() |
Remove and return the last element | Returns null | Yes |
Practical Application Scenarios
1. Checking the Queue Tail
When implementing a queue or deque, you can usepeekLast()to check the element at the tail of the queue without removing it.
2. Conditional Processing
When processing linked list data, you can first check whether the last element meets certain conditions before deciding whether to perform subsequent operations.
3. Logging
When you need to record the most recently added element of the linked list but do not want to remove it, you can use this method.
Notes
Null value handling: Since
peekLast()may returnnull, you should perform a null check before using the return value.Concurrent environment:
LinkedListis not thread-safe. UsingpeekLast()in a multi-threaded environment requires additional synchronization measures.Alternative method: If you need to ensure that the linked list is not empty, consider using
getLast(), but note that it throws when the linked list is emptyNoSuchElementException。
Summary
peekLast()The method isLinkedLista practical utility method in the class, providing a safe way to view the last element of the linked list without modifying the linked list. Understanding this method and its differences from related methods can help developers use theLinkedListclass more effectively.
Java LinkedList