I have a to filter on some nested Lists. Consider a List<A> which contains List<B> which contains List<C>. I need to filter, to return all the A Objects which contain at least one B which satisfies a given condition, and at least one C which satisfies a given condition.
I've got a contrived example below, which basically is doing what I've tried to implement in my actual example. Take a list of schools in a city Each school has many subjects, and each subject has many teachers. I want to retain a list of schools which has subject.subjectName = 'Math' and at least one teacher within this subject where teacher.age > 65.
I have implemented this using a custom predicate, so I've also made a this in my example. I'm getting an error where 'cannot convert from boolean to Stream' but honestly, I'm struggling in this scenario.
@Getter
@Setter
class School {
private String schoolId;
private List<Subject> classes;
}
@Getter
@Setter
class Subject {
String subjectName;
List<Teacher> teachers;
}
@Getter
@Setter
class Teacher {
private String teacherName;
private Integer age;
}
public class TestClass {
public static void main( String[] args ){
List<School> schools;
// Get All schools where A mathTeacher is over retirement age
List<School> schoolsWithRetirementAgeMathTeachers = schools.stream()
.filter(school -> null != school.getClasses())
.flatMap(school -> {
return school.getClasses().stream()
.filter(subject -> subject.getSubjectName().equalsIgnoreCase("Math"))
.filter(subject -> null != subject.getTeachers())
.flatMap(subject -> {
return subject.getTeachers().stream()
.filter(teacher -> teacher != null)
.anyMatch(isRetirementAge);
});
}).collect(Collectors.toList());
}
public static Predicate<Teacher> isRetirementAge = teacher -> teacher.getAge() > 65;
}
Cyou're filtering for under theByou're filtering for or are they unrelated conditions?