The replaceAll() method of java.util.Collections class is used to replace all occurrences of one specified value in a list with another. More formally, replaces with newVal each element e in the list such thatÂ
oldVal == null ? e==null : oldVal.equals(e)
Note: This method has no effect on the size of the list.
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 the list contained one or more elements e such that as shown below else falseÂ
oldVal== null ? e==null : oldVal.equals(e)
Syntax:Â
public static boolean replaceAll(List list, T oldVal, T newVal)
Example 1:
Java
// Java program to demonstrate// replaceAll() method for String valueÂ
// Importing utility classesimport java.util.*;Â
// Main classpublic class GFG {Â
    // Main driver method    public static void main(String[] argv) throws Exception    {Â
        // Try block to check for exceptions        try {Â
            // Creating a vector object of string type            List<String> vector = new Vector<String>();Â
            // Populating the above Vector object            // Custom input elements            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");Â
            // Printing elements of Vector object after            // replacing            System.out.println("Vector after replace :"                               + vector);        }Â
        // Catch block to handle the exceptions        catch (IllegalArgumentException e) {Â
            // Display message when exception occurs            System.out.println("Exception thrown : " + e);        }    }} |
Initial Vector :[A, B, A, C] Vector after replace :[TAJMAHAL, B, TAJMAHAL, C]
Example 2:
Java
// Java program to demonstrate// replaceAll() method for Integer valueÂ
// importing utility classesimport java.util.*;Â
// Main classpublic class GFG {Â
    // Main driver method    public static void main(String[] argv) throws Exception    {Â
        // Try block to check for exceptions        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 before replacing            // elements            System.out.println("Initial values are :"                               + vector);Â
            // Replacing value            // using replaceAll() method            Collections.replaceAll(vector, 20, 400);Â
            // Printing the vector after replacing elements            System.out.println("Value after replace :"                               + vector);        }Â
        // Catch block to handle IllegalArgumentException        catch (IllegalArgumentException e) {Â
            // Display the exceptions on the console            System.out.println("Exception thrown : " + e);        }    }} |
Initial values are :[20, 30, 20, 30] Value after replace :[400, 30, 400, 30]
Â
