I have a collection (as hashmap) of Doctors, into a generic Hospital class.
Map<Integer, Doctor> doctors = new HashMap<Integer, Doctor>();
For each doctor I have some information such as in the class code (focus on the patients):
public class Doctor extends Person {
private int id;
private String specialization;
private List<Person> patients = new LinkedList<Person>();
My purpose is to write this function which return busy doctors: doctors that has a number of patients larger than the average.
/**
* returns the collection of doctors that has a number of patients larger than the average.
*/
Collection<Doctor> busyDoctors(){
Collection<Doctor> doctorsWithManyPatients =
doctors.values().stream()
.map( doctor -> doctor.getPatients() )
.filter( patientsList -> { return patientsList.size() >= AvgPatientsPerDoctor; })
.collect(Collectors.toList());
return null;
}
I want to use the streams as above to perform this operation. The problem is in collect
method because at that point of usage doctorsWithManyPatients
is of type List<Collection<Person>>
and not Collection<Doctor>
. How could I do that?
Assume that AvgPatientsPerDoctor
is already defined somewhere.