set method replaces the element in the specified position with the new element. But in add(position, element) will add the element in the specified position and shifts the existing elements to right side of the array .

ArrayList<String> al = new ArrayList<String>();

    al.add("a");
    al.add(1, "b");
    System.out.println(al);

    al.set(0, "c");
    System.out.println(al);

    al.add(0, "d");
    System.out.println(al);

---------------Output -------------------------------------

[a, b]

[c, b]

[d, c, b]

Answer from girish TS on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 8 )
October 20, 2025 - The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation. Each ArrayList ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-set-method-in-java-with-examples
Java ArrayList set() Method - GeeksforGeeks
August 6, 2025 - The set() method of the ArrayList class in Java is used to replace an element at a specified position with a new value.
Discussions

replace - Arraylist - Set/ add method usage - Java - Stack Overflow
I have an Arraylist of integers. My requirement is to determine if the arraylist HAS an element existing at the specified index.If YES, then a value should be set to that index (using set method), ... More on stackoverflow.com
🌐 stackoverflow.com
When to use Sets vs Lists .
It's generally not useful to think about data structures without knowing what queries you're going to perform on them. In general, if you need to look up if the collection contains the element, use a Set. Otherwise a List will be faster for about every other operation. More on reddit.com
🌐 r/javahelp
49
14
January 23, 2022
How to set up an ArrayList object in java?
Your question is a bit confusing on wording. so I will do my best to answer your question. so an array list is just a collection. In your example case, a collection of fruit. if you want to access any fruit in that array you use indexes. FruitList.get(0).appleMethod Fruit should be an abstract class, becsuse you wouldn’t make a fruit object. Right? What is a fruit? What color is it, what shape? It makes no sense. However, an apple is a concert class. Apples are red, or green, and have pits, and so forth. Banana is another concrete class. They are both fruits. So they EXTEND the fruit class. This is called inheritance. The point of an abstract class is complex but one major point to provide functionality to a common set of concrete classes. Think animals. You could have an animal class, cat class, and a dog clAss. all animals make noise right? So in your animal class you define a method but don’t give it a method body. makeNoise(): Now when you make your dog and cat class extend animals, java will force you to finish that makeNoise method. For cat you would make it system.out.print(“meow”) for dog a bark. With these classes we make a dog and a cat object. Now, a cat couldn’t go into a dog arraylist, or a cat into a dog arraylist. Yet since they both extend animal they can go into a animal array list! then you could loop over that arrayList and call the makeNoise() method. The cat will meow, the dog will bark. Yet you just wrote one like of code. This is Polymorphism! More on reddit.com
🌐 r/AskProgramming
3
2
January 13, 2020
JAVA - Can't send update to ArrayList

I don't think this is ever going to return true:

enterPaymentConfirm == "y" || enterPaymentConfirm == "Y"

In Java you need to use equals() to compare strings, like this:

if (enterPaymentConfirm.equals("y") || enterPaymentConfirm.equals("Y")

What IDE are you using? Mine puts a red squiggly line under code if I try to compare two strings with == instead of equals because it's a common mistake.

More on reddit.com
🌐 r/learnprogramming
2
0
June 27, 2021
🌐
W3Schools
w3schools.com › java › ref_arraylist_set.asp
Java ArrayList set() Method
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> cars = new ArrayList<String>(); cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); cars.add("Mazda"); cars.set(0, "Opel"); System.out.println(cars); } }
🌐
TutorialsPoint
tutorialspoint.com › home › java/util › java arraylist set method
Java ArrayList set Method
September 1, 2008 - The Java ArrayList set(int index, E element) replaces the element at the specified position in this list with the specified element.
🌐
Medium
medium.com › @viranthrocky › understanding-the-difference-between-set-and-arraylist-in-java-4bac4b5214d9
🔍✨ Understanding the Difference Between Set and ArrayList in Java 🧠💻 | by Viranth Dharmalingam | Medium
April 11, 2025 - ✅ Use a Set when you want to eliminate duplicates and ensure your data remains unique. ✅ Choose an ArrayList when you value ordered elements and need fast, index-based access—even if duplicates are allowed.
Find elsewhere
🌐
Codecademy
codecademy.com › docs › java › arraylist › .set()
Java | ArrayList | .set() | Codecademy
June 15, 2025 - In Java, the .set() method replaces the element present at a specified position with another element in an ArrayList.
🌐
Programiz
programiz.com › java-programming › library › arraylist › set
Java ArrayList set()
And, both the methods are adding a new element to the arraylist. This is why some people consider both methods similar. However, there is a major difference between them. The set() method adds a new element at the specified position by replacing the older element at that position.
🌐
W3Schools
w3schools.com › java › java_arraylist.asp
Java ArrayList
How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum
🌐
Vultr
docs.vultr.com › java › standard-library › java › util › ArrayList › set
Java ArrayList set() - Replace Element
November 13, 2024 - The set() method in Java's ArrayList class is a crucial tool for modifying elements in a list. It allows the replacement of an element at a specified position, facilitating dynamic data management within your Java applications.
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-arraylist-set-method-example
Java ArrayList set() Method example
June 12, 2024 - Java ArrayList set() method is used to replace an existing element present in the ArrayList at the specified position with the new given element.
🌐
Quora
quora.com › Whats-the-complexity-of-set-index-element-of-an-array-list-in-Java
What's the complexity of 'set (index, element)' of an array list in Java? - Quora
Answer (1 of 2): Marco is right that set(index, element) does not increase the capacity of an ArrayList and so is definitely O(1). William extends the answer to add, and says that add is amortized O(1). I think that that is only partly right. add(element), which adds at the end of the ArrayList, ...
🌐
TutorialsPoint
tutorialspoint.com › how-do-you-turn-an-arraylist-into-a-set-in-java
How do you turn an ArrayList into a Set in Java?
May 30, 2025 - In this article, let's learn how to convert an ArrayList into a Set in Java. ArrayList is a collection that allows us to store duplicates, whereas Set is a collection that does not allow duplicates. So, when we convert an ArrayList to a Set, all the
🌐
Medium
medium.com › @menkashah060 › arraylist-list-set-map-collection-in-java-84672a156724
ArrayList, List, Set, Map(Collection)in java? | by Menkashah | Medium
January 11, 2024 - ArrayList, List, Set, Map(Collection)in java? ArrayList: In Java, ArrayList is a class. It implements the List interface It is part of the Java Collections Framework and provides dynamic arrays that …
🌐
W3Resource
w3resource.com › java-tutorial › arraylist › arraylist_set.php
Java arraylist set method - w3resource
August 19, 2022 - The ArrayList.set() method is used to set an element in an ArrayList object at the specified index.
🌐
Coderanch
coderanch.com › t › 375609 › java › Arraylist-set-index-Object-method
Arraylist set(index i, Object o) method Exception (Java in General forum at Coderanch)
January 13, 2005 - The problem here is that you don't have an array list even similar to the example you gave. In the code you gave above, you create an empty ArrayList and then try to set() an element. However, since the ArrayList is emtpy (i.e. it has no elements) you can't set() any elements.
🌐
Baeldung
baeldung.com › home › java › java collections › set vs list in java
Set vs List in Java | Baeldung
April 3, 2025 - The result shows that searching for an element in a HashSet is faster than searching for an element in an ArrayList. This ascertains that a HashSet is more efficient in a scenario where we want to search for an element in a collection in a fast and efficient way. In the previous section, we saw different metrics that measure the performance of List and Set with respect to time.
🌐
DigitalOcean
digitalocean.com › community › tutorials › set-to-list-in-java
How to Convert a Set to a List in Java: Simple and Efficient Methods | DigitalOcean
April 21, 2025 - Set<String> names = new HashSet<>(); names.add("John"); names.add("Alice"); names.add("Bob"); // Create a temporary list to hold modified elements List<String> modifiedNames = new ArrayList<>(); for (String name : names) { modifiedNames.add(name + " modified"); } // Update the original set with the modified elements names.clear(); names.addAll(modifiedNames);
🌐
Lawrence
www2.lawrence.edu › fast › GREGGJ › CMSC150 › 062ArrayLists › ArrayLists.html
Working with ArrayLists
For example, there is an Integer class corresponding to the int type and a Double class corresponding to the double type, and so on. The first step to being able to store a list of ints in an ArrayList is to instead create an ArrayList that can hold a list of Integer objects: