问题描述
我想根据values
字段过滤MyObject
的列表.在这种情况下:如果MyObject::getValues
上的任何值小于给定的value
,则谓词为false.我还没有找到使用Stream API的方法,所以我尝试了Apache
的方法.这是我的尝试:
I want to filter list of MyObject
based on values
field. In this case: if any value on MyObject::getValues
is less than given value
, than the predicate is false. I haven't found the way to do it with Stream API, so I tried ComparatorPredicate
by Apache
. Here is my try:
private Predicate<MyObject> valueGreaterThanOrEqualTo(ValueObject value){
return myObject -> myObject.getValues().stream()
.noneMatch(v -> ComparatorPredicate.comparatorPredicate(value, new ValueComparator(), ComparatorPredicate.Criterion.LESS)
.evaluate(v));
}
但是,这导致:
Caused by: java.lang.ClassCastException: Cannot cast java.lang.Boolean to com.ValueObject
at java.lang.Class.cast(Class.java:3369)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.ArrayList$ArrayListSpliterator.tryAdvance(ArrayList.java:1351)
at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.MatchOps$MatchOp.evaluateSequential(MatchOps.java:230)
at java.util.stream.MatchOps$MatchOp.evaluateSequential(MatchOps.java:196)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.noneMatch(ReferencePipeline.java:459)
所以我的问题是,为什么会发生ClassCastException,以及如何解决它和/或如何使用其他(更好的)解决方案来解决我的问题?
So my question is why does the ClassCastException occur and how to resolve it AND/OR how to resolve my problem with some other (better?) solution?
更新:
有关此问题的一些最新消息.第一个问题(ClassCastException
)是由示例中未包含的代码引起的.解决后,ComparatorPredicate
可以正常工作,因此,如果您喜欢它:继续吧.但是,我将接受已接受答案的建议,该建议更简单,并且不涉及使用外部库.尤其是因为ComparatorPredicate
不扩展java.util.function.Predicate
而是扩展org.apache.commons.collections4.Predicate
.
A little update on the issue. The first problem (ClassCastException
) was caused by the code I didn't include in my example. After solving it ComparatorPredicate
works fine, so if you like it: go for it. I, however will go with the suggestion from accepted answer which is simpler and doesn't involve using external libs. Especially since ComparatorPredicate
doesn't extend java.util.function.Predicate
but org.apache.commons.collections4.Predicate
instead.
推荐答案
我不明白您为什么必须使用ComparatorPredicate.comparatorPredicate
.
I don't see why you had to use ComparatorPredicate.comparatorPredicate
.
怎么样:
private Predicate<MyObject> valueGreaterThanOrEqualTo(ValueObject value){
ValueComparator comparator = new ValueComparator();
return myObject -> myObject.getValues().stream()
.noneMatch(v -> comparator.compare(v,value)<0);
}
这是假设ValueComparator
实现了Comparator<ValueObject>
.
这篇关于带有比较器的Java 8流过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!