use*_*840 4 java java-8 java-stream
我有许多过滤器调用流对象的方法,并使用String方法,结果取1个参数:
String s = "aComarisonString";
stream().filter( p -> p.thisReturnsAString().startsWith(s) )
stream().filter( p -> p.thisReturnsAString2().startsWith(s) )
stream().filter( p -> p.thisReturnsAString().endsWith(s) )
stream().filter( p -> p.thisReturnsAString().contains(s) )
Run Code Online (Sandbox Code Playgroud)
有没有办法生成过滤器,使它看起来/工作像
.filter( compare(thisReturnsAString,contains(s) )
.filter( compare(thisReturnsAString2,endsWith(s) )
Run Code Online (Sandbox Code Playgroud)
有几种方法,但我同意@shmosel.可读性几乎没有改善.
一种可能的解决方案:
<V, P> Predicate<? super P> compare(Function<P, V> valueFunction, Predicate<V> matchPredicate) {
return p -> matchPredicate.test(valueFunction.apply(p));
}
Run Code Online (Sandbox Code Playgroud)
对该方法的调用如下所示:
stream().filter(compare(P::thisReturnsAString, s -> s.endsWith(comparisonString)))
Run Code Online (Sandbox Code Playgroud)
P你的对象的类型在哪里.一个略微适应的变体,但可能导致许多重载方法:
<V, C, P> Predicate<? super P> compare(Function<P, V> valueFunction, BiPredicate<V, C> matchPredicate, C value) {
return p -> matchPredicate.test(valueFunction.apply(p), value);
}
Run Code Online (Sandbox Code Playgroud)
对该方法的调用可能如下所示:
stream().filter(compare(P::thisReturnsAString, String::endsWith, comparisonString))
Run Code Online (Sandbox Code Playgroud)
这里没有真正的收获.只是另一种写作方式,你用更多的样板代码写的东西;-)
编辑:添加@shmosel关于谓词与函数的建议
| 归档时间: |
|
| 查看次数: |
5270 次 |
| 最近记录: |