我需要使用基于布尔函数的谓词来组合流操作.通过将方法的参数重新抛出为谓词找到了一种解决方法,如下所示:
public <T> Predicate<T> pred(final Predicate<T> aLambda) {
return aLambda;
}
public List<String> foo() {
return new ArrayList<String>().stream() //of course, this does nothing, simplified
.filter(pred(String::isEmpty).negate())
.collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)
'pred'方法似乎无能为力,但不是这样:
public List<String> foo() {
return new ArrayList<String>().stream()
.filter((String::isEmpty).negate())
.collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)
也没有任何内联转换:
public List<String> foo() {
return new ArrayList<String>().stream()
.filter(((Predicate)String::isEmpty).negate())
.collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)
似乎工作.失败了
此表达式的目标类型必须是功能接口
在'pred(...)'方法中,花式转换发生了什么?