Easy Tutorial
❮ File Size Thread Monitor ❯

Java Example - List Element Replacement

Java Examples

The following example demonstrates how to use the replaceAll() method of the Collections class to replace all occurrences of a specified element 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]

Java Examples

❮ File Size Thread Monitor ❯