Collections replaceAll() method in Java with Examples
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); } } } |
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); } } } |
Initial values are :[20, 30, 20, 30] Value after replace :[400, 30, 400, 30]
Recommended Posts:
- CopyOnArrayList replaceAll() method in Java with Examples
- HashMap replaceAll(BiFunction) method in Java with Examples
- Matcher replaceAll(String) method in Java with Examples
- Matcher replaceAll(Function) method in Java with Examples
- Collections max() method in Java with Examples
- Collections min() method in Java with Examples
- Collections synchronizedSortedMap() method in Java with Examples
- Collections synchronizedSortedSet() method in Java with Examples
- Collections checkedSet() method in Java with Examples
- Collections checkedSortedSet() method in Java with Examples
- Collections copy() method in Java with Examples
- Collections checkedSortedMap() method in Java with Examples
- Collections enumeration() method in Java with Examples
- Collections list() method in Java with Examples
- Collections synchronizedCollection() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



