我有以下课程:
public class Insurance {
...
}
Run Code Online (Sandbox Code Playgroud)
public class Customer {
private List<Insurance> insurances;
public List<Insurance> getInsurances() {
return insurances;
}
...
}
Run Code Online (Sandbox Code Playgroud)
public class CustomerRegistry {
private List<Customer> customers;
...
}
Run Code Online (Sandbox Code Playgroud)
以及这个帮助方法,它将a减少List<Predicate<T>>为单个Predicate<T>:
public Predicate<T> reducePredicates(List<Predicate<T>> predicates) {
return predicates.stream()
.reduce(Predicate::and)
.orElse(p -> true);
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是获得与过滤器列表匹配的保险列表,这些过滤器属于与过滤器列表匹配的客户.如果不清楚,下面的代码将有希望澄清.
方法在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))) …Run Code Online (Sandbox Code Playgroud)