Java LinkedList set() Method
LinkedListis an important class in the Java Collections Framework, it implementsListinterface.set()method isLinkedLista very useful method provided by the class, used to replace the element at the specified position in the linked list.
In simple terms,set()method allows you to:
- Access a specific position in the linked list by index
- Replace the old element at that position with a new element
- Return the old element that was replaced
Method Syntax
set()The complete syntax of the method is as follows:
Parameter Description:
index: The position of the element to be replaced (counting from 0)element: The new element to be stored at the specified position
Return Value:
- Returns the old element that was replaced
Usage Example
Let's understand through a complete code exampleset()the usage of the method:
Example
public class LinkedListSetExample {
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");
fruits.add("Date");
System.out.println("Original LinkedList: " + fruits);
// Use the set() method to replace the second element
String replaced = fruits.set(1, "Blueberry");
System.out.println("Replaced element: " + replaced);
System.out.println("Modified LinkedList: " + fruits);
}
}
Output:
原始链表: [Apple, Banana, Cherry, Date] 被替换的元素: Banana 修改后的链表: [Apple, Blueberry, Cherry, Date]
Notes
Index Out of Bounds Exception
If the specified index is out of the linked list range (index < 0 or index >= size()),set()the method will throwIndexOutOfBoundsException。
Example
fruits.set(4, "Fig");
Performance Considerations
SinceLinkedListis implemented based on a linked list,set()the method needs to traverse the linked list to find the element at the specified position, therefore:
- The average time complexity is O(n)
- For scenarios requiring frequent random access,
ArrayListmay be a better choice
Null Values Not Allowed
IfLinkedListdoes not allow null elements (depending on the specific implementation), attempting to set a null value may throwNullPointerException。
Comparison with Other Methods
set() vs add()
set(): Replaces the element at the specified positionadd(): Inserts a new element at the specified position, shifting the original element backward
set() vs get()
set(): Modifies the element at the specified position and returns the old valueget(): Only retrieves the element at the specified position without modifying the linked list
Practical Application Scenarios
set()The method is particularly useful in the following scenarios:
- Updating specific elements in a linked list
- Replacing history records when implementing undo/redo functionality
- Modifying intermediate step data in algorithms
- Updating object states in game development
Summary
LinkedListofset()The method is a simple yet powerful tool that allows developers to precisely modify specific elements in a linked list. Understanding how this method works and its limitations will help you use it more effectively in actual programming.LinkedListcollections.
Java LinkedList