You can use the set() method of java.util.ArrayList
class to replace an existing element of ArrayList in Java. The set(int
index, E element) method takes two parameters, first is the index of an
element you want to replace and second is the new value you want to
insert.
You can use this method as long as your ArrayList is not immutable e.g. not created using Collections.unmodifiableList(),
in such case the set() method throws java.lang.UnsupportedOperationExcepiton.
Though, you can also use the set() method with the List returned by Arrays.asList() method as oppose to add() and remove()
which is not supported there. You just need to be careful with the
index of elements. For example, if you want to replace the first element
then you need to call set(0, newValue) because similar to an array, ArrayList index is also zero based.
Now, the questions come why do you want to replace an element in the ArrayList? Why not just remove the element and insert a new one? Well, obviously the remove and insert will take more time than replace. The java.util.ArrayList provides O(1) time performance for replacement, similar to size(), isEmpty(), get(), iterator(), and listIterator() operations which runs in constant time.
Now, you may wonder that why set() gives O(1) performance but add() gives O(n) performance, because it could trigger resizing of array, which involves creating a new array and copying elements from old array to new array. See Core Java Volume 1 - Fundamentals to learn more about implementation and working of ArrayList in Java.
Now, the questions come why do you want to replace an element in the ArrayList? Why not just remove the element and insert a new one? Well, obviously the remove and insert will take more time than replace. The java.util.ArrayList provides O(1) time performance for replacement, similar to size(), isEmpty(), get(), iterator(), and listIterator() operations which runs in constant time.
Now, you may wonder that why set() gives O(1) performance but add() gives O(n) performance, because it could trigger resizing of array, which involves creating a new array and copying elements from old array to new array. See Core Java Volume 1 - Fundamentals to learn more about implementation and working of ArrayList in Java.
Replacing an existing object in ArrayList
Here is an example of replacing an existing value from ArrayList in
Java. In this example, I have an ArrayList of String which contains
names of some of the most popular and useful books for Java programmers. Our example replaces the 2nd element of the ArrayList by calling the ArrayList.set(1, "Introduction to Algorithms") because the index of the array starts from zero. You should read a comprehensive book like "Big Java Early Object" by Cay S. Horstmann to learn more about useful collection classes in Java, including ArrayList.
Java Program to replace elements in ArrayList
import java.util.ArrayList;
import java.util.List;
/*
* Java Program to demonstrate how to replace existing value in
* ArrayList.
*/
public class ArrayListSetDemo {
public static void main(String[] args) {
// let's create a list first
List<String> top5Books = new ArrayList<String>();
top5Books.add("Clean Code");
top5Books.add("Clean Coder");
top5Books.add("Effective Java");
top5Books.add("Head First Java");
top5Books.add("Head First Design patterns");
// now, suppose you want to replace "Clean Coder" with
// "Introduction to Algorithms"
System.out.println("ArrayList before replace: " + top5Books);
top5Books.set(1, "Introductoin to Algorithms");
System.out.println("ArrayList after replace: " + top5Books);
}
}
Output
ArrayList before replace: [Clean Code, Clean Coder, Effective Java,
Head First Java, Head First Design patterns]
ArrayList after replace: [Clean Code, Introduction to Algorithms,
Effective Java, Head First Java, Head First Design patterns]
You can see that initially, we have a list of 5 books and we have replaced the second element by calling set(1, value) method, hence in the output, the second book which was "Clean Coder" was replaced by "Introduction to Algorithms".
That's all about how to replace existing elements of ArrayList in Java. The set() method is perfect to replace existing value just make sure that List you are using is not immutable. You can also use this method with any other List type e.g. LinkedList. The time complexity is O(n) because we are doing index based access to the element.
No comments:
Post a Comment