我想更改中的所有项目list
.
正确的做法是什么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);
}
}
您可以使用replaceAll http://docs.oracle.com/javase/8/docs/api/java/util/List.html#replaceAll-java.util.function.UnaryOperator-.
将此列表的每个元素替换为应用
该元素的运算符。
ArrayList<String> l = new ArrayList<>(Arrays.asList("AB","A","AA"));
l.replaceAll(x -> "b" + x);
System.out.println(l);
Output:
[bAB, bA, bAA]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)