Collections replaceAll() method in Java with Examples

The replaceAll() method of java.util.Collections class is used to Replaces all occurrences of one specified value in a list with another. More formally, replaces with newVal each element e in list such that (oldVal==null ? e==null : oldVal.equals(e)). (This method has no effect on the size of the list.)

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)).

Below are the examples to illustrate the replaceAll() method

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// replaceAll() method for String value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // creating object of List<String>
            List<String> vector = new Vector<String>();
  
            // populate the vector
            vector.add("A");
            vector.add("B");
            vector.add("A");
            vector.add("C");
  
            // printing the vector
            System.out.println("Initial Vector :" + vector);
  
            // replacing value
            // using replaceAll() method
            Collections.replaceAll(vector, "A", "TAJMAHAL");
  
            System.out.println("Vector after replace :" + vector);
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

Initial Vector :[A, B, A, C]
Vector after replace :[TAJMAHAL, B, TAJMAHAL, C]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// replaceAll() method for Integer value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // creating object of List<String>
            List<Integer> vector = new Vector<Integer>();
  
            // populate the vector
            vector.add(20);
            vector.add(30);
            vector.add(20);
            vector.add(30);
  
            // printing the vector
            System.out.println("Initial values are :" + vector);
  
            // replacing value
            // using replaceAll() method
            Collections.replaceAll(vector, 20, 400);
  
            System.out.println("Value after replace :" + vector);
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

Initial values are :[20, 30, 20, 30]
Value after replace :[400, 30, 400, 30]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.