我可以使用类似:
.forEach(System.out::print)
...打印我的列表项,但是如果在打印之前我还要执行其他操作,则不能像这样使用它:
mylist.replaceAll(s -> s.toUpperCase()).forEach(System.out::print)
我收到一个错误:
无效不能被取消引用
你必须决定。当您想要修改列表时,您无法合并这些操作。然后,您需要两个语句。
myList.replaceAll(String::toUpperCase);// modifies the list
myList.forEach(System.out::println);
如果您只想map
在打印前就赋值而不修改列表,则必须使用Stream
:
myList.stream().map(String::toUpperCase).forEachOrdered(System.out::println);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句