我正在尝试将列表中的所有字符串替换为其等效的大写字母的方法。我知道可以使用replaceAll()方法。但是,我只是想知道是否也可以使用forEach()方法?
List<String> list2 = Arrays.asList("justin", "nancy", "cooc", "amy", "david");
list2.replaceAll(s -> s.toUpperCase());
list2.forEach();
是否可以使用forEach()方法将列表中的所有字符串替换为其等效的大写字母?
是的1,如果您将其与结合使用IntStream.range(int, int)
。就像是,
IntStream.range(0, list2.size()).forEach(x ->
list2.set(x, list2.get(x).toUpperCase()));
1但是,您当前的解决方案效率更高。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句