How can I apply a collection of predicates to a Java stream?
The following code shall illustrate what I have in mind:
final Collection<Predicate<MyObject>> filters = getFilters();
final Stream<MyObject> oStream = getMyObjectStream();
oStream
.filterAll(filters) // <- this one doesn't exist
.forEach(System.out::println);
I'm currently using a function .filter(m -> applyAllFilters(m, filters)) with a classic loop, but wonder if there is a more "streamy" way?
boolean applyAllFilters(final MyObject m, final Collection<Predicate<MyObject>> filters) {
Iterator Predicate<MyObject> iter = filters.iterator();
while(iter.hasNext()) {
Predicate<MyObject> p = iter.next();
if (!p.test(m)) {
return false;
}
}
return true;
}