Java Examples - List Element Replacement
The following example demonstrates how to use the replaceAll() method of the Collections class to replace all specified elements in a List:
Main.java File
import java.util.*;
public class Main {
public static void main(String[] args) {
List list = Arrays.asList("one Two three Four five six one three Four".split(" "));
System.out.println("List :" + list);
Collections.replaceAll(list, "one", "hundrea");
System.out.println("replaceAll: " + list);
}
}
The output of the above code is:
List :[one, Two, three, Four, five, six, one, three, Four]
replaceAll: [hundrea, Two, three, Four, five, six, hundrea, three, Four]
YouTip