如何在java8中的字符串列表中更改项目 [英] how to change items in a list of string in java8
本文介绍了如何在java8中的字符串列表中更改项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想更改list
中的所有项目.
用java8
正确的方法是什么?
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);
输出:
[bAB, bA, bAA]
这篇关于如何在java8中的字符串列表中更改项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文