Open In App

Replacing All Occurrences of Specified Element of Java ArrayList

Last Updated : 15 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

ArrayList is present in java.util package and it is the implementation class of List interface. It does allow the duplicates elements and also it maintains the insertion order of the elements. It dynamically resized its capacity. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed.

Example: In this program, we will first create an ArrayList of Integers and add elements using add() method, Now we will iterate over the ArrayList elements using the for each loop.

Java




import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
class GFG {
    public static void main(String[] args)
    {
        // ArrayList of Integers
        ArrayList<Integer> gfg = new ArrayList<>();
 
        // adding members into the list now
        gfg.add(1);
        gfg.add(20);
        gfg.add(21);
        gfg.add(13);
        gfg.add(21);
        gfg.add(10);
        gfg.add(21);
 
        // printing all the elements using for each loop
        for (Integer value : gfg) {
            System.out.println(value);
        }
    }
}


Output

1
20
21
13
21
10
21

Approach(Using replaceAll() method)

In this program, we will create an ArrayList of Integers and add elements in it using add() method, then we will replace all occurrences of 21 with 200 using replaceAll() method.

Syntax:

public static  boolean replaceAll(List list, T oldVal, T newVal)

Parameters: This method takes the following argument as a Parameter

  • list: the list in which replacement is to occur.
  • oldVal: the old value to be replaced.
  • newVal: the new value with which oldVal is to be replaced.

Return Value: This method returns true if list contained one or more elements e such that (oldVal==null ? e==null : oldVal.equals(e)).

Example 1:

Java




// Java program for Replacing All Occurrences of Specified
// Element of ArrayList
 
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
 
class GFG {
    public static void main(String[] args)
    {
        // ArrayList of Integers
        ArrayList<Integer> gfg = new ArrayList<>();
 
        // adding members into the list now
        gfg.add(1);
        gfg.add(20);
        gfg.add(21);
        gfg.add(13);
        gfg.add(21);
        gfg.add(10);
        gfg.add(21);
 
        // replacing 21 with 100 using replaceAll() method
        Collections.replaceAll(gfg, 21, 200);
 
        // printing all the elements using for each loop
        for (Integer value : gfg) {
            System.out.println(value);
        }
    }
}


Output

1
20
200
13
200
10
200

Example 2:

Java




// Java program to demonstrate
// replaceAll() method for Integer value
 
import java.util.*;
 
public class GFG {
    public static void main(String[] argv) throws Exception
    {
        // creating object of List<String>
        List<String> list = new ArrayList<String>();
 
        // populate the vector
        list.add("?");
        list.add("For");
        list.add("?");
 
        // printing the vector
        System.out.println("Initial values are :" + list);
 
        // replacing value
        // using replaceAll() method
        Collections.replaceAll(list, "?", "Geeks");
 
        System.out.println("Value after replace :" + list);
    }
}


Output

Initial values are :[?, For, ?]
Value after replace :[Geeks, For, Geeks]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads