AbstractList set() Method in Java with Examples
The set() method of java.util.AbstractList class is used to replace any particular element in the abstract list created using the AbstractList class with another element. This can be done by specifying the position of the element to be replaced and the new element in the parameter of the set() method.
Syntax:
AbstractList.set(int index, Object element)
Parameters: This function accepts two parameters as described below:
- index: This is of integer type and refers to the position of the element that is to be replaced from the abstract list.
- element: It is the new element by which the existing element will be replaced and is of the same object type as the abstract list.
Return Value: The method returns the previous value from the abstract list that is replaced with the new value.
Below program illustrate the AbstractList.set() method:
// Java code to illustrate set() import java.util.*; import java.util.LinkedList; public class AbstractListDemo { public static void main(String args[]) { // Creating an empty AbstractList AbstractList<String> list = new LinkedList<String>(); // Use add() method to add elements in the list list.add("Geeks"); list.add("for"); list.add("Geeks"); list.add("10"); list.add("20"); // Displaying the AbstractList System.out.println("AbstractList:" + list); // Using set() method to replace Geeks with GFG System.out.println("The Object that is replaced is: " + list.set(2, "GFG")); // Using set() method to replace 20 with 50 System.out.println("The Object that is replaced is: " + list.set(4, "50")); // Displaying the modified AbstractList System.out.println("The new AbstractList is:" + list); } } |
AbstractList:[Geeks, for, Geeks, 10, 20] The Object that is replaced is: Geeks The Object that is replaced is: 20 The new AbstractList is:[Geeks, for, GFG, 10, 50]
Program 2:
// Java code to illustrate set() import java.util.*; public class LinkedListDemo { public static void main(String args[]) { // Creating an empty AbstractList AbstractList<Integer> list = new LinkedList<Integer>(); // Use add() method to add elements in the list list.add(10); list.add(20); list.add(30); list.add(40); list.add(50); // Displaying the AbstractList System.out.println("AbstractList:" + list); // Using set() method to replace 10 with 100 System.out.println("The Object that is replaced is: " + list.set(0, 100)); // Using set() method to replace 20 with 200 System.out.println("The Object that is replaced is: " + list.set(1, 200)); // Displaying the modified AbstractList System.out.println("The new AbstractList is:" + list); } } |
AbstractList:[10, 20, 30, 40, 50] The Object that is replaced is: 10 The Object that is replaced is: 20 The new AbstractList is:[100, 200, 30, 40, 50]
Recommended Posts:
- AbstractList get() method in Java with Examples
- AbstractList add(E ele) method in Java with Examples
- AbstractList lastIndexOf() method in Java with Examples
- AbstractList addAll() method in Java with Examples
- AbstractList clear() method in Java with Examples
- AbstractList equals() method in Java with Examples
- AbstractList hashCode() method in Java with Examples
- AbstractList iterator() method in Java with Examples
- AbstractList indexOf() method in Java with Examples
- AbstractList listIterator() Method in Java with Examples
- AbstractList subList() method in Java with Examples
- AbstractList remove() Method in Java with Examples
- AbstractList add(int index, E element) method in Java with Examples
- AbstractList in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
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.



