Replace all occurrences of specified element of ArrayList - Java Collection Framework

Java examples for Collection Framework:ArrayList

Description

Replace all occurrences of specified element of ArrayList

Demo Code


import java.util.ArrayList;
import java.util.Collections;
 
public class Main {
  public static void main(String[] args) {
    ArrayList arrayList = new ArrayList();
   //from   w  w w  .  j a va  2s.c o  m
    arrayList.add("A");
    arrayList.add("B");
    arrayList.add("A");
    arrayList.add("C");
    arrayList.add("D");
 
    System.out.println("ArrayList Contains : " + arrayList);
   
    Collections.replaceAll(arrayList, "A","Replace All");
   
    System.out.println("After Replace All, ArrayList Contains : " + arrayList);
  }
}

Result


Related Tutorials