我有以下课程:
public class Insurance {
...
}
public class Customer {
private List<Insurance> insurances;
public List<Insurance> getInsurances() {
return insurances;
}
...
}
public class CustomerRegistry {
private List<Customer> customers;
...
}
以及此辅助方法,可将List<Predicate<T>>
缩减为单个Predicate<T>
:
public Predicate<T> reducePredicates(List<Predicate<T>> predicates) {
return predicates.stream()
.reduce(Predicate::and)
.orElse(p -> true);
}
我想要做的是获取与过滤器列表匹配的保险列表,这些过滤器属于与过滤器列表匹配的客户。如果不清楚,下面的代码将有希望澄清。
方法在上面的CustomerRegistry
类中。
public List<Insurance> findInsurances(List<Predicate<Customer>> cusPredicates,
List<Predicate<Insurance>> insPredicates) {
List<Insurance> matches = new LinkedList<>();
customers.stream()
.filter(reducePredicates(cusPredicates)
.forEach(cus -> cus.getInsurances()
.stream()
.filter(reducePredicates(insPredicates))
.forEach(cus -> matches.add(cus)))
return matches;
}
如果没有matches
列表,有没有办法做到这一点?我可以执行某种减少,以便直接返回匹配的保险(即不添加到matches
这样的临时集合中吗?
答案 0 :(得分:10)
使用flatMap():
customers.stream()
.filter(reducePredicates(cusPredicates))
.flatMap(cus -> cus.getInsurances().stream())
.filter(reducePredicates(insPredicates))
.collect(Collectors.toList())
或者更好,避免一遍又一遍地减少谓词:
Predicate<Customer> customerPredicate = reducePredicates(cusPredicates);
Predicate<Customer> insurancePredicate = reducePredicates(insPredicates);
List<Insurance> =
customers.stream()
.filter(customerPredicate)
.flatMap(cus -> cus.getInsurances().stream())
.filter(insurancePredicate)
.collect(Collectors.toList())