Java 8 lambda filtering based on condition as well as order

后端 未结 4 745
清酒与你
清酒与你 2021-01-01 13:36

I was trying to filter a list based on multiple conditions, sorting.

class Student{
        private int Age;
        private String className;
        privat         


        
4条回答
  •  太阳男子
    2021-01-01 14:18

    Just to mix and merge the other solutions, you could alternatively do :

    Map nameToStudentMap = new HashMap<>();
    Set finalListOfStudents = students.stream()
            .map(x -> nameToStudentMap.merge(x.getName(), x, (a, b) -> a.getAge() > b.getAge() ? a : b))
            .collect(Collectors.toSet());
    

提交回复
热议问题