【发布时间】:2020-01-29 06:05:50
【问题描述】:
我正在尝试 forEach 中的方法引用
private static void printConditionally8(List<Person> people, Predicate<Person> predicate) {
people.forEach(p-> { if (predicate.test(p)){
System.out.println("Print here");}
});
}
上面工作正常,但我想使用方法参考使它更短,但是它给出了编译问题。有什么办法让它发生吗?
private static void printConditionally8(List<Person> people, Predicate<Person> predicate) {
people.forEach({ if (predicate::test){
System.out.println("Print here");}
});
}
【问题讨论】:
-
我不确定,但谓词中的 P 是否应该大写?
-
很好的问题,但我不知道答案:(
标签: java lambda java-8 method-reference