CopyOnWriteArrayList set() method in Java with Examples
The set(E e) method in the class CopyOnWriteArrayList class replaces the element at the specified index with the element provided as parameter to the method. The method returns the element that has been replaced by the new element.
Syntax:
public E set(int index, E element)
Parameters: The method takes two parameters mentioned below:
- Index: Contains the position at which the new element is to replace the existing one. It is mandatory to add.
- Element: Contains the new element to be replaced with.
Return Value: The method returns the element that has been replaced.
Exceptions: The method throws IndexOutOfBoundsException occurs when the method has an index which is either less than 0 or greater than the size of the list.
Below are some programs to illustrate the use of CopyOnWriteArrayList.set() method:
Program 1:
// Program to illustrate the use of set() method import java.util.concurrent.CopyOnWriteArrayList; public class CopyOnWriteArrayListDemo { public static void main(String[] args) { // creating an ArrayList CopyOnWriteArrayList<String> arrayList = new CopyOnWriteArrayList<String>(); // Adding elements to the list arrayList.add(0, "geeks"); arrayList.add(1, "for"); arrayList.add(2, "geeksforgeeks"); // before invoking the set() method System.out.println("CopyOnWriteArrayList: " + arrayList); // invoking the set() method String returnValue = arrayList.set(0, "toodles"); // printing the returned value System.out.println("The value returned " + "on caling set() method:" + returnValue); // print CopyOnWriteArrayList System.out.println("CopyOnWriteArrayList " + "after calling set():" + arrayList); } } |
CopyOnWriteArrayList: [geeks, for, geeksforgeeks] The value returned on caling set() method: geeks CopyOnWriteArrayList after calling set(): [toodles, for, geeksforgeeks]
Program 2:
// Program to illustrate the ArrayIndexOutOfBoundsException import java.util.concurrent.CopyOnWriteArrayList; public class CopyOnWriteArrayListDemo { public static void main(String[] args) { // creating an ArrayList CopyOnWriteArrayList<String> arrayList = new CopyOnWriteArrayList<String>(); // Adding elements to the list arrayList.add(0, "geeks"); arrayList.add(1, "for"); arrayList.add(2, "geeksforgeeks"); // before invoking the set() method System.out.println("CopyOnWriteArrayList: " + arrayList); try { System.out.println("Trying to add " + "element at index 4 " + "usiung set() method"); // invoking the set() method String returnValue = arrayList.set(4, "toodles"); // printing the returned value } catch (Exception e) { System.out.println(e); } } } |
CopyOnWriteArrayList: [geeks, for, geeksforgeeks] Trying to add element at index 4 usiung set() method java.lang.ArrayIndexOutOfBoundsException: 4
Recommended Posts:
- CopyOnWriteArrayList remove() method in Java with Examples
- CopyOnWriteArrayList removeAll() method in Java with Examples
- CopyOnWriteArrayList addAll() method in Java with Examples
- CopyOnWriteArrayList removeIf() method in Java with Examples
- CopyOnWriteArrayList retainAll() method in Java with Examples
- CopyOnWriteArrayList forEach() method in Java with Examples
- CopyOnWriteArrayList addAllAbsent() method in Java with Examples
- CopyOnWriteArrayList equals() method in Java with Examples
- CopyOnWriteArrayList subList() method in Java with Examples
- CopyOnWriteArrayList add() method in Java
- CopyOnWriteArrayList get() method in Java
- CopyOnWriteArrayList contains() method in Java
- CopyOnWriteArrayList lastIndexOf() method in Java
- CopyOnWriteArrayList toArray() method in Java
- CopyOnWriteArrayList isEmpty() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ManasChhabra2



