Can a lambda be used to change a List's values in-place ( without creating a new list)?

    List<String> list = Arrays.asList("Bob", "Steve", "Jim", "Arbby");
    list.replaceAll(String::toUpperCase);

With a popular library Guava you can create a computing view on the list which doesn't allocate memory for a new array, i.e.:

upperCaseStrings = Lists.transform(strings, String::toUpperCase)

A better solution is proposed by @StuartMarks, however, I am leaving this answer as it allows to also change a generic type of the collection.


Another option is to declare a static method like mutate which takes list and lambda as a parameter, and import it as a static method i.e.:


mutate(strings, String::toUpperCase);

A possible implementation for mutate:

@SuppressWarnings({"unchecked"})
public static  List mutate(List list, Function function) {
    List objList = list;

    for (int i = 0; i