Java LinkedList set() Method

Java LinkedList Java LinkedList


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:

  1. Access a specific position in the linked list by index
  2. Replace the old element at that position with a new element
  3. Return the old element that was replaced

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 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

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 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

// This will throw an exception because index 4 does not exist (the linked list has only 4 elements, index range is 0-3)
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 position
  • add(): 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 value
  • get(): 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:

  1. Updating specific elements in a linked list
  2. Replacing history records when implementing undo/redo functionality
  3. Modifying intermediate step data in algorithms
  4. 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 Java LinkedList

Other Extensions