AbstractList set() Method in Java with Examples Last Updated : 26 Nov, 2018 Comments Improve Suggest changes Like Article Like Report 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 // 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); } } Output: 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 // 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); } } Output: 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] Comment More infoAdvertise with us Next Article AbstractList set() Method in Java with Examples chinmoy lenka Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions Java-AbstractList +2 More Practice Tags : JavaJava-CollectionsMisc Similar Reads AbstractList in Java with Examples The AbstractList class in Java is a part of the Java Collection Framework and implements the Collection interface and the AbstractCollection class. AbstractList class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a Rand 7 min read AbstractList add(E ele) method in Java with Examples The add(E ele) method of AbstractList class in Java is used to insert the specified element to the end of the current list. Syntax: public boolean add(E ele) Where E is the type of element maintained by this AbstractList collection. Parameter: This method accepts a single parameter ele which represe 2 min read AbstractList addAll() method in Java with Examples The addAll() method of java.util.AbstractList class is used to insert all of the elements in the specified collection into this list at the specified position. This shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elem 5 min read AbstractList clear() method in Java with Examples The clear() method of java.util.AbstractList class is used to remove all of the elements from this list. The list will be empty after this call returns. Syntax: public void clear() Returns Value: This method does not return anything. Below are the examples to illustrate the clear() method. Example 1 2 min read AbstractList equals() method in Java with Examples The equals() method of java.util.AbstractList class is used to compare the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e 3 min read AbstractList get() method in Java with Examples The get() method of java.util.AbstractList class is used to return the element at the specified position in this list. Syntax: public abstract E get(int index) Parameters: This method takes index of the element as a parameter, the element at which is to be returned. Returns Value: This method return 2 min read AbstractList hashCode() method in Java with Examples The hashCode() method of java.util.AbstractList class is used to return the hash code value for this list. Syntax: public int hashCode() Returns Value: This method returns the hash code value for this list. Below are the examples to illustrate the hashCode() method. Example 1: Java // Java program t 2 min read AbstractList indexOf() method in Java with Examples The indexOf() method of java.util.AbstractList class is used to return the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if t 3 min read AbstractList iterator() method in Java with Examples The iterator() method of java.util.AbstractList class is used to return an iterator over the elements in this list in proper sequence. This implementation returns a straightforward implementation of the iterator interface, relying on the backing list's size(), get(int), and remove(int) methods. Synt 2 min read AbstractList lastIndexOf() method in Java with Examples The lastIndexOf() method of java.util.AbstractList class is used to return the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 2 min read Like