I am in the process of trying to better understand how to use Java streams. I have these classes:
public class Plan {
List<Request> requestList;
}
public class Request {
List<Identity> identityList;
boolean isCancelled;
}
public class Identity {
String idNumber;
}
I am trying to write a method that returns the plan that contains the non-cancelled request with a matching identity number.
This is what I have tried:
public static Plan findMatchingPlan(List<Plan> plans, String id) {
List<Plan> filteredPlan = plans.stream()
.filter(plan -> plan.getRequestList().stream()
.filter(request -> !request.isCancelled())
.filter(request -> request.getIdentityList().stream()
.filter(identity -> identity.getIdNumber().equals(id))))
.collect(Collectors.toList());
}
This gives me an error:
java.util.stream.Stream<com.sandbox.Identity> cannot be converted to boolean
I sort of understand why there is an error. The nested filter returns a filter that cannot be evaluated as a boolean. The problem is, I don't know what I am missing.
Any help would be appreciated.
Plan(return type)( or aList<Plan>*(local variable). If just a singlePlan, do you want first, any, or last match?