Java LinkedList set() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, set(index, e) replaces the element at a position in the LinkedList with e and returns the element that was there before. The list keeps the same number of elements.

That is the difference from add(index, e), which inserts a new element and pushes the others back. With set nothing moves; one slot simply gets a new value.

An instance method of java.util.LinkedList (since Java 1.2): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by List, and LinkedList implements it.

Syntax

Pass the position and the new value.

Java
LinkedList<String> list = new LinkedList<>();
list.add("A");
list.add("B");
list.add("C");
String old = list.set(1, "X");   // old is "B", list is [A, X, C]
CallWhat it does
list.set(i, e)Replaces the element at i and returns the old one.
list.get(i)Reads the element at i without changing it.
list.add(i, e)Inserts a new element and makes the list longer.
listIterator.set(e)Replaces the last element returned by the iterator.

The returned value is the old element, which is handy for swapping or for remembering what was replaced.

Library declarations
Java
// java.util.LinkedList
public E set(int index, E element)

Parameters

ParameterDescription
index (int)The position to replace, from 0 to size() - 1.
element (E)The new value. It may be null.

Return Value

Return ValueDescription
EThe element that used to be at that index.

Compare with Similar Methods

Ways to change a list:

MethodHow it differs
add(index, e)Inserts a new element and shifts the later ones back.
get(index)Reads the element at an index.
replaceAll(op)Replaces every element by applying a function.
ArrayList.set()The same method on an array-backed list, where reaching the index is instant.

Java Examples

Example 1 — Replace an Element and Keep the Old Value

Replace the second item of a list and print the value that was replaced.

Java
import java.util.LinkedList;

public class LinkedListSetDemo {
    public static void main(String[] args) {
        LinkedList<String> menu = new LinkedList<>();
        menu.add("soup");
        menu.add("pasta");
        menu.add("cake");

        String old = menu.set(1, "pizza");

        System.out.println("replaced: " + old);
        System.out.println("menu: " + menu);
        System.out.println("size: " + menu.size());
    }
}

How It Works

  1. Three dishes. "pasta" is at index 1.
  2. set swaps the value. "pizza" takes the place of "pasta" at index 1.
  3. The old value comes back. old holds "pasta", the element that was replaced.
  4. The size is unchanged. There are still three dishes, because set does not insert anything.

Example 2 — Swap Two Elements with set

Use the returned old value to swap the first and last elements of a list.

Java
import java.util.LinkedList;

public class LinkedListSetSwap {
    public static void main(String[] args) {
        LinkedList<Integer> nums = new LinkedList<>();
        nums.add(10);
        nums.add(20);
        nums.add(30);
        nums.add(40);
        System.out.println("before: " + nums);

        int last = nums.size() - 1;
        int firstValue = nums.get(0);
        int lastValue = nums.set(last, firstValue);
        nums.set(0, lastValue);

        System.out.println("after:  " + nums);
    }
}

How It Works

  1. Four numbers. The first is 10 and the last is 40.
  2. Remember the first value. get(0) saves 10 in a variable.
  3. The last slot gets 10. set(last, firstValue) puts 10 at the end and returns the old last value, 40.
  4. The first slot gets 40. The second set finishes the swap, so the list ends as 40, 20, 30, 10.

Example 3 — See Which Indexes set Accepts

Try the last valid index, then size(), and then set on an empty list.

Java
import java.util.LinkedList;

public class LinkedListSetRange {
    public static void main(String[] args) {
        LinkedList<String> slots = new LinkedList<>();
        slots.add("free");
        slots.add("free");

        slots.set(1, "taken");
        System.out.println("slots: " + slots);

        try {
            slots.set(2, "taken");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("set(size()) -> IndexOutOfBoundsException");
        }

        slots.clear();
        try {
            slots.set(0, "taken");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("set on empty -> IndexOutOfBoundsException");
        }
    }
}

How It Works

  1. Two slots. The valid indexes for set are 0 and 1.
  2. Index 1 works. The second slot becomes "taken".
  3. Index equal to size fails. Index 2 does not hold an element, and set cannot create one, so it throws IndexOutOfBoundsException.
  4. An empty list has no valid index. Even set(0, ...) fails, so use add to put the first element in.

Frequently Asked Questions

Call list.set(index, newValue). It replaces the element at that index and returns the old one.
set replaces an element and keeps the size. add inserts a new element and makes the list one longer.
The element that was previously at that index.
The index must refer to an existing element. Use add(e) to append at the end.
Save one value with get, put it into the other position with set (which returns the old value), and then set that old value into the first position. Collections.swap(list, i, j) does it in one call.

Next: size()

Return the number of elements in the list.

size() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful