Program
import java.util.ArrayList; import java.util.Collections; public class ArrayListReplaceAll { public static void main(String[] args) { ArrayList<String> colorList = new ArrayList<String>(); colorList.add("White"); colorList.add("White"); colorList.add("Black"); colorList.add("Grey"); colorList.add("Blue"); colorList.add("White"); colorList.add("Green"); colorList.add("Black"); colorList.add("White"); System.out.println("Original List:" + colorList); Collections.replaceAll(colorList, "White", "Black"); System.out.println(" After replacing all White color with Balack color :"+ colorList); } }
Output
Original List[White, White, Black, Grey, Blue, White, Green, Black, White] After replacing all White color with Black color :[Black, Black, Black, Grey, Blue,Black, Green, Black, Black]
Description
public void replaceAll(UnaryOperator operator)
List Replaces each element of this list with the result of applying the operator to that element.