Java Program to Replace Elements in a List


In this article, we will understand how to replace elements in a list. The List extends Collection and declares the behavior of a collection that stores a sequence of elements.

The Collection is a framework that provides architecture to store and manipulate the group of objects. Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion.

Below is a demonstration of the same −

Suppose our input is

Input list : [Java, Scala, Mysql, Redshift]

The desired output would be

After replacing one element of the list: [Java, Scala, Python, Redshift]

Algorithm

Step 1 - START
Step 2 - Declare a list namely input_list.
Step 3 - Define the values.
Step 4 - Use the function .set() and pass the index to be updated and the string to be added as parameter.
Step 5 - Display the list as result
Step 6 - Stop

Example 1

Here, we bind all the operations together under the ‘main’ function.

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      System.out.println("The required packages have been imported");
      try {
         ArrayList<String> input_list = new ArrayList<>();
         input_list.add("Java");
         input_list.add("Scala");
         input_list.add("Mysql");
         input_list.add("Redshift");
         System.out.println("The list is defined as" +input_list);
         input_list.set(2, "Python");
         System.out.println("\nAfter replacing one element of the list: " +input_list);
      }
      catch (Exception e) {
         System.out.println(e);
      }
   }
}

Output

The required packages have been imported
The list is defined as[Java, Scala, Mysql, Redshift]

After replacing one element of the list: [Java, Scala, Python, Redshift]

Example 2

Here, we encapsulate the operations into functions exhibiting object oriented programming.

import java.util.*;
public class Demo {
   static void replace(List<String> input_list, String input_string){
      input_list.set(2, input_string);
      System.out.println("\nAfter replacing one element of the list: " +input_list);
   }
   public static void main(String[] args) {
      System.out.println("The required packages have been imported");
      try {
         ArrayList<String> input_list = new ArrayList<>();
         input_list.add("Java");
         input_list.add("Scala");
         input_list.add("Mysql");
         input_list.add("Redshift");
         System.out.println("The list is defined as" +input_list);
         String input_string = "Python";
         replace(input_list, input_string);
      }
      catch (Exception e) {
         System.out.println(e);
      }
   }
}

Output

The required packages have been imported
The list is defined as[Java, Scala, Mysql, Redshift]

After replacing one element of the list: [Java, Scala, Python, Redshift]

Updated on: 30-Mar-2022

143 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements