使用java 8 api打印列表项 [英] Print list items with java 8 api
本文介绍了使用java 8 api打印列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以使用类似的东西:
I can use something like :
.forEach(System.out::print)
打印我的列表项但是如果我在打印前还有其他操作要做,我不能像以下那样使用它:
to print my list items but if I have another operation to do before printing I can't use it like :
mylist.replaceAll(s -> s.toUpperCase()).forEach(System.out::print)
我收到错误:无法取消引用
推荐答案
你必须决定。如果要修改列表,则无法组合操作。那么你需要两个语句。
You have to decide. When you want to modify the list, you can’t combine the operations. You need two statements then.
myList.replaceAll(String::toUpperCase);// modifies the list
myList.forEach(System.out::println);
如果您只想 map
打印前的值而不修改列表,你必须使用 Stream
:
If you just want to map
values before printing without modifying the list, you’ll have to use a Stream
:
myList.stream().map(String::toUpperCase).forEachOrdered(System.out::println);
这篇关于使用java 8 api打印列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文