Skip to content
Related Articles

Related Articles

Improve Article
Collections replaceAll() method in Java with Examples
  • Last Updated : 08 Oct, 2018

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:




// 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);
        }
    }
}
Output:
Initial Vector :[A, B, A, C]
Vector after replace :[TAJMAHAL, B, TAJMAHAL, C]

Example 2:




// 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);
        }
    }
}
Output:
Initial values are :[20, 30, 20, 30]
Value after replace :[400, 30, 400, 30]

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :