Java LinkedList peekLast() Method
peekLast() is a method in Java's LinkedList is a method provided by the class for viewing but not removing the last element of the list. If the list is empty, this method returns null.
Syntax format
E peekLast()
Method Characteristics
1. Non-destructive operation
peekLast() method only views the last element of the list and does not remove it from the list.
2. Empty list handling
When the list is empty,peekLast() returns null instead of throwing an exception.
3. Time complexity
Because LinkedList : Returns null when the list is emptypeekLast() method has a time complexity of O(1).
Method parameters and return value
Parameters
This method does not accept any parameters.
Return Value
- Returns the last element of the list
- If the list is empty, it returns
null
Usage examples
Basic Usage
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
(
"Cherry"
)
;
String
lastElement
=
list.
peekLast
(
)
;
System
.
out
.
println
(
"Last element: "
lastElement
)
;
// Output: Cherry
System
.
out
.
println
(
"LinkedList after peek: "
list
)
;
// The list remains unchanged
}
}
Empty list situation
Example
import
java.util.LinkedList
;
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 with related methods
| Method Names | Description | Behavior when the list is empty | Whether the list is modified |
|---|---|---|---|
peekLast() |
View the last element | Returns null | No |
getLast() |
get the last element | Throwing an Exception | No |
removeLast() |
Remove and return the last element | Throwing an Exception | Is |
pollLast() |
Remove and return the last element | Returns null | Is |
Practical Application Scenarios
1. Check the tail of the queue
When implementing a queue or deque, you can use peekLast() to check the element at the tail of the queue without removing it.
2. Conditional processing
When processing 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 list without removing it, you can use this method.
Notes
-
Null Value Handling: Because
peekLast()may returnnull, you should perform a null check before using the return value. -
Concurrent Environment:
LinkedListis not thread-safe; using it in a multi-threaded environmentpeekLast()Additional synchronization measures are needed. -
Alternative methods: If you need to ensure the list is not empty, you can consider using
getLast(), but note that it throwsNoSuchElementException.
Summary
peekLast() The method is LinkedList is a practical utility method in the class that provides a safe way to view the last element of the list without modifying it. Understanding this method and its difference from related methods can help developers use LinkedList class.
Java LinkedList