Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java—使用流获取具有特定属性的列表元素_Java_Java Stream - Fatal编程技术网

Java—使用流获取具有特定属性的列表元素

Java—使用流获取具有特定属性的列表元素,java,java-stream,Java,Java Stream,我有两个名为学校和学生的班级,一组学校,一份学生名单和两张地图,其中包含学校对学生的偏好,反之亦然: Set<School> schoolSet = new TreeSet<>(); Collections.addAll(schoolSet, schools); List<Student> studentList = new LinkedList<>(Arrays.asList(students)); Map<School, List&l

我有两个名为
学校
学生
的班级,一组学校,一份学生名单和两张地图,其中包含学校对学生的偏好,反之亦然:

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<>();
Set schoolSet=new TreeSet();
集合。添加所有(学校集、学校);
List studentList=newlinkedlist(Arrays.asList(students));
Map=newtreemap();
Map stdPrefMap=newhashmap();
我使用不同的实现,因为这是我必须完成的任务之一

我正在尝试使用java streams创建一个查询,该查询返回集合中的学校,这些学校将某个学生作为其最高优先级,因此该学生的索引在该特定学校密钥的值列表中应为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());
List target=Arrays.asList(学校[0],学校[2]);
列表结果=studentList.stream()
.filter(std->stdPrefMap.get(std.containsAll(目标))
.collect(Collectors.toList());

我如何使用streams获得优先选择某个学生的学校?

这将实现您指定的目标:

Student target = ...
schoolSet.stream()
        .filter(school -> {
            List<Student> preferredStudents = schoolPrefMap.get(school);
            return !preferredStudents.isEmpty() && preferredStudents.get(0).equals(target);
        }).collect(Collectors.toList());
学生目标=。。。 schoolSet.stream() .filter(学校->{ List preferredStudents=schoolPrefMap.get(学校); return!preferredStudents.isEmpty()&&preferredStudents.get(0).equals(目标); }).collect(Collectors.toList()); 我可以问一下,为什么不使用Map和Map(因为它非常适合这种带有分级首选项的情况,并且在列表上使用contains()/containsAll()不是很有效)

在这种情况下,您的代码如下所示:

schoolSet.stream()
        .filter(school -> {
            Queue<Student> preferredStudents = schoolPrefMap.get(school);
            return !preferredStudents.isEmpty() && preferredStudents.peek().equals(target);
        }).collect(Collectors.toList());
schoolSet.stream()
.filter(学校->{
Queue preferredStudents=schoolPrefMap.get(学校);
return!preferredStudents.isEmpty()&&preferredStudents.peek().equals(目标);
}).collect(Collectors.toList());

这将完成您指定的任务:

Student target = ...
schoolSet.stream()
        .filter(school -> {
            List<Student> preferredStudents = schoolPrefMap.get(school);
            return !preferredStudents.isEmpty() && preferredStudents.get(0).equals(target);
        }).collect(Collectors.toList());
学生目标=。。。 schoolSet.stream() .filter(学校->{ List preferredStudents=schoolPrefMap.get(学校); return!preferredStudents.isEmpty()&&preferredStudents.get(0).equals(目标); }).collect(Collectors.toList()); 我可以问一下,为什么不使用Map和Map(因为它非常适合这种带有分级首选项的情况,并且在列表上使用contains()/containsAll()不是很有效)

在这种情况下,您的代码如下所示:

schoolSet.stream()
        .filter(school -> {
            Queue<Student> preferredStudents = schoolPrefMap.get(school);
            return !preferredStudents.isEmpty() && preferredStudents.peek().equals(target);
        }).collect(Collectors.toList());
schoolSet.stream()
.filter(学校->{
Queue preferredStudents=schoolPrefMap.get(学校);
return!preferredStudents.isEmpty()&&preferredStudents.peek().equals(目标);
}).collect(Collectors.toList());

我希望它对您有所帮助:

        List<School> schoolsWithTargetedStudentAsTopPref = schoolSet.stream()
            .filter(school -> schoolPrefMap.get(school).stream()
                    .findFirst()
                    .filter(student-> student.equals(target))
                    .map(optionalSchool -> !optionalSchool.isEmpty())
                    .get())
            .collect(Collectors.toList());
List schoolswithtargetedstudentatstoppref=schoolSet.stream()
.filter(school->schoolPrefMap.get(school.stream)()
.findFirst()
.filter(学生->学生.equals(目标))
.map(optionalSchool->!optionalSchool.isEmpty())
.get())
.collect(Collectors.toList());

我希望它对您有所帮助:

        List<School> schoolsWithTargetedStudentAsTopPref = schoolSet.stream()
            .filter(school -> schoolPrefMap.get(school).stream()
                    .findFirst()
                    .filter(student-> student.equals(target))
                    .map(optionalSchool -> !optionalSchool.isEmpty())
                    .get())
            .collect(Collectors.toList());
List schoolswithtargetedstudentatstoppref=schoolSet.stream()
.filter(school->schoolPrefMap.get(school.stream)()
.findFirst()
.filter(学生->学生.equals(目标))
.map(optionalSchool->!optionalSchool.isEmpty())
.get())
.collect(Collectors.toList());