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 running the above code is:
List :[one, Two, three, Four, five, six, one, three, Four] replaceAll: [hundrea, Two, three, Four, five, six, hundrea, three, Four]Other Extensions
Java Examples