Check out the set(int index, E element) method in the List interface

Answer from TotoroTotoro on Stack Overflow
๐ŸŒ
Java Code Geeks
javacodegeeks.com โ€บ home โ€บ core java
Java ArrayList Insert/Replace At Index - Java Code Geeks
January 2, 2022 - Use the ArrayList.add(int index, Object value) method to add any object or element at the specific index of ArrayList and use ArrayList.set(int index, E value) to replace the value at the specific index of ArrayList in java.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-replace-a-element-in-java-arraylist
How to Replace a Element in Java ArrayList? - GeeksforGeeks
July 23, 2025 - To replace an element in Java ArrayList, set() method of java.util. An ArrayList class can be used. The set() method takes two parameters the indexes of the element that has to be replaced and the new element. The index of an ArrayList is zero-based.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java list โ€บ replace element at a specific index in a java arraylist
Replace Element at a Specific Index in a Java ArrayList | Baeldung
April 3, 2025 - This position is what we call the index. Then, we can replace the old element with a new one. The most common way to replace an element in Java ArrayList is to use the set (int index, Object element) method.
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ java collections โ€บ how to replace an element in java arraylist
How to Replace an Element in Java ArrayList
October 11, 2023 - The method returns the same ArrayList element that is just replaced. import java.util.ArrayList; import java.util.List; public class DriverClass { public static void main(String[] args) { List <String> weekDays = new ArrayList<>(); weekDays.add("Monday"); weekDays.add("Monday"); weekDays.add("Wednesday"); weekDays.add("Thursday"); weekDays.add("Friday"); weekDays.add("Saturday"); weekDays.add("Sunday"); System.out.println("Week Days (original) : " + weekDays + "\n"); String replacingText = "Tuesday"; String replacedText = weekDays.set(1, replacingText); System.out.println("Replacing Text: " + replacingText); System.out.println("Replaced Text: " + replacedText + "\n"); System.out.println("Week Days (updated) : " + weekDays); } }
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ collections framework โ€บ java arraylist โ€บ replace an existing item in arraylist
Replace an Existing Item in ArrayList
January 12, 2023 - Java ArrayList ยท Learn to update or replace an existing element in ArrayList with a new specified element or value, using set (int index, Object newItem) method. To replace an existing item, we must find the itemโ€™s exact position (index) ...
๐ŸŒ
Vultr
docs.vultr.com โ€บ java โ€บ standard-library โ€บ java โ€บ util โ€บ ArrayList โ€บ set
Java ArrayList set() - Replace Element | Vultr Docs
November 13, 2024 - Ensure you have an ArrayList created and populated with initial values. Determine the index at which you want to replace an element. Use the set() method to replace the element at the specified index.
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ library โ€บ arraylist โ€บ replaceall
Java ArrayList replaceAll()
Rather, it replaces all value of the arraylist with new values from operator. import java.util.ArrayList; class Main { public static void main(String[] args) { // create an ArrayList ArrayList<String> languages = new ArrayList<>(); // add elements to the ArrayList languages.add("java"); ...
Find elsewhere
๐ŸŒ
Java67
java67.com โ€บ 2016 โ€บ 08 โ€บ how-to-replace-element-of-arraylist-in-java.html
How to replace an element of ArrayList in Java? Example | Java67
The set() method is perfect to replace existing values just make sure that the List you are using is not immutable. You can also use this method with any other List type like LinkedList.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ How-to-replace-an-element-of-an-ArrayList-in-Java
How to replace an element in a list using Java
July 30, 2019 - Following example uses replaceAll() method to replace all the occurance of an element with a different element in a list. import java.util.*; public class Main { public static void main(String[] args) { List list = Arrays.asList("one Two three ...
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_arraylist_replaceall.asp
Java ArrayList replaceAll() Method
The replaceAll() method replaces every item in a list with the result of performing an operation on the item. The operation can be defined by a lambda expression that is compatible with Java's UnaryOperator interface.
๐ŸŒ
JavaGoal
javagoal.com โ€บ home โ€บ how to replace element in an arraylist?
replace element in an ArrayList and set() method - JavaGoal
July 27, 2020 - How to replace element in an ArrayList and how we can use the set(int index,E element) method. This method replace the existing element
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-replace-an-element-in-a-list
Java Program to Replace an Element in a List - GeeksforGeeks
January 7, 2026 - Example: This program shows how to replace a specific element in a list using the replaceAll() method with a condition. ... import java.util.ArrayList; class GFG { public static void main(String[] args) { ArrayList<Integer> list1 = new ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ replacing-all-occurrences-of-specified-element-of-java-arraylist
Replacing All Occurrences of Specified Element of Java ArrayList - GeeksforGeeks
July 23, 2025 - In this program, we will create an ArrayList of Integers and add elements in it using add() method, then we will replace all occurrences of 21 with 200 using replaceAll() method. ... // Java program for Replacing All Occurrences of Specified ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ replace-an-element-from-arraylist-using-java-listiterator
Replace an Element From ArrayList using Java ListIterator - GeeksforGeeks
April 3, 2023 - ArrayList<String> name = new ArrayList<>(); name.add("Yash"); name.add("Akash"); name.add("Amar"); name.add("Abhishek"); name.add("Rajnikanth"); Let us assume that we need to replace "Rajnikanth" with "Mohit". Now, using a ListIterator, we will iterate to the last element of the list and then replace it - ListIterator<String> iterator = name.listIterator(); while(iterator.hasNext()) { iterator.next(); } iterator.set("Mohit"); ... // java program to replace an element // from ArrayList using Java ListIterator import java.util.ArrayList; import java.util.ListIterator; class GFG { public static v
๐ŸŒ
W3Resource
w3resource.com โ€บ java-tutorial โ€บ arraylist โ€บ arraylist_replaceall.php
Java ArrayList replaceAll Method - w3resource
Java ArrayList.replaceAll() Method with example: The replaceAll() method is used to replace each element of this list with the result of applying the operator to that element. Errors or runtime exceptions thrown by the operator are relayed to ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ replace-all-elements-of-arraylist-with-with-java-collections
Replace All Elements Of ArrayList with with Java Collections
In order to replace all elements of ArrayList with Java Collections, we use the Collections.fill() method. The static void fill(List list, Object element) method replaces all elements in the list with the specified element in the argument.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ replace-an-element-in-an-arraylist-using-the-listiterator-in-java
Replace an element in an ArrayList using the ListIterator in Java
An element in ArrayList can be replaced using the ListIterator method set(). This method has a single parameter i.e. the element that is to be replaced and the set() method replaces it with the last element returned by the next() or previous() methods. A program that demonstrates this is given ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ replace-all-occurrences-of-specified-element-of-arraylist-with-java-collections
Replace all occurrences of specified element of ArrayList with Java Collections
June 25, 2020 - In order to replace all occurrences of specified element of ArrayList with Java Collections, we use the Collections.replaceAll() method. This method returns true if list contains one or more elements e such that (oldVal==null ? e==null : oldVal.eq