Close

Java Collections - Collections.replaceAll() Examples

Java Collections Java Java API 


Class:

java.util.Collections

java.lang.Objectjava.lang.Objectjava.util.Collectionsjava.util.CollectionsLogicBig

Method:

public static <T> boolean replaceAll(List<T> list,
                                     T oldVal,
                                     T newVal)

Replaces all occurrences of one specified value in a list with another.


Examples


package com.logicbig.example.collections;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ReplaceAllExample {

public static void main(String... args) {
List<String> list = Arrays.asList("one", "two", "one");
System.out.println(list);
boolean b = Collections.replaceAll(list, "one", "three");
System.out.println(b);
System.out.println(list);
}
}

Output

[one, two, one]
true
[three, two, three]




See Also