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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
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:
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]
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]