使用 Java Streams 过滤嵌套列表
我有一个嵌套列表上的过滤。考虑包含list< b>
的lt; a>
,其中包含list> list< c>
。我需要过滤,返回所有包含满足给定条件的B的A对象,至少一个满足给定条件的C。
我在下面有一个人为的示例,基本上是在实际示例中尝试实现的工作。请列出每个学校的城市学校清单,每个学科都有许多学科,并且有许多老师。我想保留一个学科的学校列表。主题名='数学',在这个主题中至少有一位老师。 65。
我已经使用自定义谓词实现了此功能,因此在我的示例中我也做了一个。 我遇到了一个错误,“无法从布尔人转换为流式传输”,但是说实话,在这种情况下我正在挣扎。
@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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您尝试返回父对象,则不想使用
flatMap()
。您只需要嵌套一些anyMatch()
调用:If you're trying to return the parent objects, you don't want to use
flatMap()
. You just need to nest someanyMatch()
calls:由于您正在寻找“学校”作为输出,因此不需要将流
映射
/平面映射
到另一个对象中。过滤功能必须足够智能,能够根据您的要求遍历嵌套集合并确认是否存在anyMatch
。主要在下面的代码中,我们过滤了所有存在名为“数学”的科目并且其中一位教授数学的教师超过上述退休年龄的学校。
Since you are looking for
School
s as an output, you do not need tomap
/flatmap
your stream into another object. Thefilter
ing capability has to be smart enough to go through the nested collections and confirm if there wasanyMatch
based on your requirements.Primarily in the below code, we filter all those schools where a subject by the name "Math" is present and one of the teachers teaching Maths is above the mentioned retirement age.