Java LinkedList set() Method
LinkedList is an important class in the Java Collections Framework; it implements List interface.set() The method is LinkedList is a very useful method provided by the class for replacing the element at a specified position in the list.
Simply put,set() The method allows you to:
- Access a specific position in the list by index
- Replace the old element at that position with a new element
- Return the replaced old element
Method Syntax
set() The complete syntax of the method is as follows:
public
E set
(
int
index, E element
)
Parameter Description:
index: The position of the element to replace (counting from 0)element: The new element to store at the specified position
Return Value:
- Return the replaced old element
Usage examples
Let us understand through a complete code example set() the use of the method:
Example
import
java.util.LinkedList
;
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 linked list: "
fruits
)
;
// Use the set() method to replace the second element
String
replaced
=
fruits.
set
(
1
,
"Blueberry"
)
;
System
.
out
.
println
(
"After replacement: "
replaced
)
;
System
.
out
.
println
(
"Modified list: "
fruits
)
;
}
}
Output result:
原始链表: [Apple, Banana, Cherry, Date]
被替换的元素: Banana
修改后的链表: [Apple, Blueberry, Cherry, Date]
Notes
Index out-of-bounds exception
If the specified index is out of the list range (index < 0 or index >= size()),set() method throws IndexOutOfBoundsException.
Example
// This will throw an exception because index 4 does not exist (the list has only 4 elements, index range is 0-3)
fruits.
set
(
4
,
"Fig"
)
;
Performance considerations
Because LinkedList is implemented based on a linked list,set() method needs to traverse the list to find the element at the specified position, so:
- The average time complexity is O(n)
- For scenarios that require frequent random access,
ArrayListmay be a better choice
Does not allow null values
If both the LinkedList does not allow null elements (depending on the specific implementation); attempting to set a null value may throw NullPointerException.
Comparison with Other Methods
set() vs add()
set(): Replaces the element at the specified positionadd(): Inserts a new element at the specified position, shifting existing elements backward
set() vs get()
set(): Modifies the element at the specified position and returns the old valueget(): Only gets the element at the specified position without modifying the list
Practical Application Scenarios
set() The method is especially useful in the following scenarios:
- Update specific elements in the list
- Replace history records when implementing undo/redo features
- Modify intermediate-step data in algorithms
- Update object state in game development
Summary
LinkedList 's set() method is a simple but powerful tool that allows developers to precisely modify specific elements in the list. Understanding how this method works and its constraints will help you use LinkedList collections.
Java LinkedList