Java LinkedList addLast() Method
addLast()The method is aLinkedListcommon method provided by the LinkedList class in Java, used to add elements at the end of the list. This method is particularly useful for implementing queue (FIFO) data structures.
public void addLast(E e)
Method Parameters
| Parameter | Type | Description |
|---|---|---|
| e | E | The element to be added at the end of the linked list |
Return Value
This method has no return value (voidvoid type).
Method Features
- Time complexity: O(1) - because
LinkedListIt maintains pointers to the head and tail nodes, so elements can be added directly at the end. - Thread-unsafe: This method is not thread-safe; external synchronization is required if used in a multithreaded environment.
- Allows null values: You can add
nullelements to the linked list - Capacity limit:
LinkedListThere is no capacity limit; elements can be added until memory is exhausted.
Usage Example
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 linked list
System.out.println(fruits); // Output: [Apple, Banana, Cherry]
}
}
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 linked 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]
// 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]
Exceptions
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()It can be used to implement the enqueue operation of a queue. - Logging: When recording events in chronological order, this method can be used.
- History: When implementing undo/redo functionality, a linked list can be used to store 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);
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 beLinkedListmore efficient than - In Java,
LinkedListimplementsDequethe interface, so you can also useofferLast()the method to achieve similar functionality - If the linked list is empty,
addLast()the added element will become the first and only element of the linked list.
Summary
LinkedListofaddLast()The addLast() method is a simple but powerful tool for efficiently adding elements at the end of a linked 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