Java 将字符串列表的所有元素转换为大写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35395317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Transform all elements of a list of strings to upper case
提问by lapots
I want to transform a list of strings to upper case.
我想将字符串列表转换为大写。
Here's my code to do this:
这是我执行此操作的代码:
List<String> list = Arrays.asList("abc", "def", "ghi");
List<String> upped = list.stream().map(String::toUpperCase).collect(Collectors.toList());
Is there a simpler/better way to do this?
有没有更简单/更好的方法来做到这一点?
采纳答案by Bohemian
You have not actually transformed the list; you have created a new list.
你实际上并没有改变列表;您创建了一个新列表。
To transform the list in one small method call, use List#replaceAll()
:
要在一个小的方法调用中转换列表,请使用List#replaceAll()
:
list.replaceAll(String::toUpperCase);