How can I apply multiple predicates to a java.util.Stream's
filter()
method?
This is what I do now, but I don't really like it. I have a Collection
of things and I need to reduce the number of things based on the Collection
of filters (predicates):
Collection<Thing> things = someGenerator.someMethod();
List<Thing> filtered = things.parallelStream().filter(p -> {
for (Filter f : filtersCollection) {
if (f.test(p))
return true;
}
return false;
}).collect(Collectors.toList());
I know that if I knew number of filters up-front, I could do something like this:
List<Thing> filtered = things.parallelStream().filter(filter1).or(filter2).or(filter3)).collect(Collectors.toList());
But how can I apply unknown number of predicates without mixing programming styles? For know it looks sort of ugly...
See Question&Answers more detail:os