Intersection of Two Lists Objects in java 8

前端 未结 2 1060
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-06 08:17

An intersection of Two Lists Objects in java 8. Can some tell me what am I doing wrong?

List originalStudent = new ArrayList<>();
List&l         


        
2条回答
  •  灰色年华
    2021-01-06 09:05

    What you can do is construct a SortedSet from the two concatenated lists originalStudent and newStudent. The sorted set uses a Comparator.comparing(Student::getName).thenComparing(Student::getLastName) as its comparator.

    Stream.concat(originalStudent.stream(), newStudent.stream())
        .collect(Collectors.toCollection(() -> new TreeSet<>(
            Comparator.comparing(Student::getFname)
                .thenComparing(Student::getLname))
        ))
    

提交回复
热议问题