有两个类似于这样的普通对象,分别是学生(Student)和课程(Course):
public class Student {
List<Course> courses;
...
}
public class Course {
String name;
...
}
如果我们有一个学生列表(list
of Students
),如何通过他们课程的名称来筛选一些学生?
- 首先我尝试使用
flatMap
来回答这个问题,但它返回的是课程对象而不是学生对象。 - 然后我使用
allMatch
(以下代码)。然而,它返回的学生列表总是为空。问题出在哪里?
List<Student> studentList;
List<Student> AlgorithmsCourserStudentList = studentList.stream().
filter(a -> a.stream().allMatch(c -> c.getCourseName.equal("Algorithms"))).
collect(Collectors.toList());