VOOZH about

URL: https://www.geeksforgeeks.org/java/collections-replaceall-method-in-java-with-examples/

⇱ Collections replaceAll() Method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Collections replaceAll() Method in Java with Examples

Last Updated : 22 Apr, 2022

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:


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

Example 2:


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

 
Comment