让我们假设有一个狐狸类,它已经得到了名称,颜色和年龄。让我们假设我有狐狸的名单,我想打印出那些狐狸的名字,它的颜色是绿色。我想用流这样做。
领域:
我写了下面的代码做过滤和SYSOUT:
foxes.stream().filter(fox -> fox.getColor().equals("green"))
.forEach(fox -> System.out::println (fox.getName()));
不过,也有我的代码中的一些语法问题。
问题是什么?我应该如何梳理出来?
你不能用lambda表达式结合方法引用,只需使用一个:
foxes.stream()
.filter(fox -> fox.getColor().equals("green"))
.forEach(fox -> System.out.println(fox.getName()));
或其他:
foxes.stream()
.filter(fox -> fox.getColor().equals("green"))
.map(Fox::getName) // required in order to use method reference in the following terminal operation
.forEach(System.out::println);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句