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