Open In App

Collections replaceAll() Method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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 classes
import java.util.*;
 
// Main class
public 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);
        }
    }
}


Output

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 classes
import java.util.*;
 
// Main class
public 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);
        }
    }
}


Output: 

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

 



Last Updated : 22 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads