【发布时间】:2021-03-15 22:30:14
【问题描述】:
我有两个名为 School 和 Student 的班级、一组学校、一个学生列表和两张地图,其中包含学校在学生方面的偏好,反之亦然:
Set<School> schoolSet = new TreeSet<>();
Collections.addAll(schoolSet, schools);
List<Student> studentList = new LinkedList<>(Arrays.asList(students));
Map<School, List<Student>> schoolPrefMap = new TreeMap<>();
Map<Student, List<School>> stdPrefMap = new HashMap<>();
我正在使用不同的实现,因为这是我必须完成的任务之一。
我正在尝试使用 java 流创建一个查询,该流从集合中返回以某个学生为最高优先级的学校,因此该学生的索引在该特定学校键的值列表中应该为 0 .
我有一个尝试类似的例子,只让至少有特定学校的学生在他的偏好中
List<School> target = Arrays.asList(schools[0], schools[2]);
List<Student> result = studentList.stream()
.filter(std -> stdPrefMap.get(std).containsAll(target))
.collect(Collectors.toList());
我如何使用流来获取以特定学生为首选的学校?
【问题讨论】:
标签: java java-stream