I want to change all items in list
.
What is the correct way to do it with java8
?
public class TestIt {
public static void main(String[] args) {
ArrayList<String> l = new ArrayList<>();
l.add("AB");
l.add("A");
l.add("AA");
l.forEach(x -> x = "b" + x);
System.out.println(l);
}
}
You can use replaceAll
.
Replaces each element of this list with the result of applying the operator to that element.
ArrayList<String> l = new ArrayList<>(Arrays.asList("AB","A","AA"));
l.replaceAll(x -> "b" + x);
System.out.println(l);
Output:
[bAB, bA, bAA]
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments