A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/abstractlist-set-method-in-java-with-examples/ below:

AbstractList set() Method in Java with Examples

AbstractList set() Method in Java with Examples

Last Updated : 11 Jul, 2025

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:

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]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4