Java LinkedList addLast() Method
addLast() The method is in Java's LinkedList is a commonly used method provided by the class for adding elements to the end of the list. This method is particularly useful for implementing queue (FIFO) data structures.
public void addLast(E e)
Method parameters
| Parameters | Type | Description |
|---|---|---|
| e | E | The element to be added to the end of the list |
Return Value
This method has no return value (void type).
Method Characteristics
- Time Complexity: O(1) - because
LinkedListmaintains pointers to the head and tail nodes, so elements can be added directly at the end - Not thread-safe: This method is not thread-safe and requires external synchronization if used in a multi-threaded environment
- Allows null Values: Can add to the list
nullElement - Capacity limit:
LinkedListThere is no capacity limit; elements can be added until memory is exhausted
Usage examples
Basic Usage
Example
import
java.util.LinkedList
;
public
class
LinkedListDemo
{
public
static
void
main
(
String
[
]
args
)
{
// Create a LinkedList
LinkedList
<
String
fruits
=
new
LinkedList
<>
(
);
// Use the addLast() method to add elements
fruits.
addLast
(
"Apple"
)
;
fruits.
addLast
(
"Banana"
)
;
fruits.
addLast
(
"Cherry"
)
;
// Print the list
System
.
out
.
println
(
fruits
)
;
// Output: [Apple, Banana, Cherry]
}
}
Comparison with the add() method
Example
LinkedList
<
Integer
numbers
=
new
LinkedList
<>
(
);
// addLast() and add() are equivalent in LinkedList
numbers.
add
(
1
)
;
// Add at the end
numbers.
addLast
(
2
)
;
// Add at the end
System
.
out
.
println
(
numbers
)
;
// Output: [1, 2]
Exception cases
addLast() The method may throw the following exceptions:
- ClassCastException - If the class of the specified element does not allow it to be added to this list
- NullPointerException - If the list does not allow null elements and the specified element is null
- IllegalArgumentException - If some property of this element does not allow it to be added to this list
Practical Application Scenarios
- Implementing a queue:
addLast()can be used to implement the enqueue operation of a queue - Logging: This method can be used when recording events in chronological order
- History records: When implementing undo/redo functionality, a linked list can be used to store the operation history
Example
// Simple queue implementation example
LinkedList
<
String
queue
=
new
LinkedList
<>
(
);
// Enqueue operation
queue.
addLast
(
"Task1"
)
;
queue.
addLast
(
"Task2"
)
;
queue.
addLast
(
"Task3"
)
;
// Dequeue operation
String
firstTask
=
queue.
removeFirst
(
)
;
System
.
out
.
println
(
"Processing: "
firstTask
)
;
Notes
- For frequent random access operations,
ArrayListmay be more efficient thanLinkedListmore efficient - In Java,
LinkedListimplements theDequeinterface, so you can also useofferLast()method to achieve similar functionality - If the list is empty,
addLast()it will add the new element as the first and only element of the list
Summary
LinkedList 's addLast() The method is a simple but powerful tool for efficiently adding elements at the end of a list. Understanding how this method works and its applicable scenarios can help you make the right data structure choice when you need to frequently add elements at the end of a collection.
Java LinkedList