Java LinkedList getLast() Method · Private Archive
getLast() is a method in Java's LinkedList Java LinkedList getLast() Method java.util.LinkedList is a method provided by the class for getting the last element in the list. This method belongs to
Method Definition
public E getLast()
- Return value typeclass library and is one of the commonly used methods when operating on linked list data structures.
- : Generic E (returns the last element in the list)Exception thrown
NoSuchElementException
Use Cases
: If the list is empty, it throws
- When you need to:
- Check the element at the end of the list without removing it
- Implement the peek operation of a queue (view but do not remove the tail element)
Basic Usage Example
Example
import
java.util.LinkedList
;
public
class
GetLastExample
{
public
static
void
main
(
String
[
]
args
)
{
// Create a LinkedList
LinkedList
<
String
fruits
=
new
LinkedList
<>
(
);
// Add elements
fruits.
add
(
"Apple"
)
;
fruits.
add
(
"Banana"
)
;
fruits.
add
(
"Cherry"
)
;
In algorithms that need frequent access to the tail element of the list
String
lastFruit
=
fruits.
getLast
(
)
;
System
.
out
.
println
(
// Use getLast() to get the last element
lastFruit
)
;
}
}
Output result:
最后一个水果是: Cherry
Notes
"The last fruit is: "
Example
LinkedList
<
Integer
emptyList
=
new
LinkedList
<>
(
);
1. Empty list handling
int
last
=
emptyList.
getLast
(
)
;
// This line of code will throw NoSuchElementException:
Example
if
(
!
emptyList.
isEmpty
(
)
)
{
int
last
=
emptyList.
getLast
(
)
;
}
else
{
System
.
out
.
println
(
Safe usage
)
;
}
"The list is empty!"
getLast()2. The difference from peekLast()peekLast(): Throws an exception if the list is empty
Performance considerations
LinkedList : Returns null when the list is emptygetLast() is a doubly linked list implementation,
Practical application cases
method has a time complexity of O(1) because it directly accesses the tail node of the list.
Example
LinkedList
<
String
history
=
new
LinkedList
<>
(
);
Implement a simple history record feature
history.
add
(
// Add browsing records
)
;
history.
add
(
"Home"
)
;
history.
add
(
"Products"
)
;
"Cart"
String
lastVisited
=
history.
getLast
(
)
;
System
.
out
.
println
(
// Get the most recently visited page
lastVisited
)
;
Frequently Asked Questions
"Your last visited page is: "
getLast()Q1: What is the difference between getLast() and getFirst()?getFirst()Get the last element of the list
Get the first element of the list
Because LinkedList implements the Deque Q2: Why is a separate getLast() method needed?
interface and needs to efficiently support double-ended operations.
- Q3: How to avoid getLast() throwing an exception?
list.isEmpty() - Use
peekLast()First check whether the list is empty:
Summary
getLast() The method is LinkedList method as an alternative getLast() or peekLast() is a simple but practical method in the class that helps developers quickly access the last element of the list. Understanding this method's behavior and potential exceptions is very important for writing robust code. In actual development, choose based on specific needs
Java LinkedList