Replaces the element at the specified position in this list with the specified element.

package com.logicbig.example.arraylist;
import java.util.*;
public class SetExample {
public static void main(String... args) {
// Replace element at specific position
ArrayList<String> list = new ArrayList<>(Arrays.asList("A", "B", "C"));
String oldValue = list.set(1, "B-New");
System.out.println("Old value: " + oldValue);
System.out.println("Updated list: " + list);
}
}
Output
Old value: B
Updated list: [A, B-New, C]
JDK 25