Java 8 lambda filtering based on condition as well as order

后端 未结 4 732
清酒与你
清酒与你 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:04

    If you need a grouping only sorted, it is quite simple:

    Map> collect = students.stream() // stream capabilities
            .sorted(Comparator.comparingInt(Student::getAge).reversed()) // sort by age, descending
            .collect(Collectors.groupingBy(Student::getName)); // group by name.
    

    Output in collect:

    • Prince=[Student [Age=24, className=B, Name=Prince]],
    • Smith=[Student [Age=24, className=A, Name=Smith]],
    • John=[Student [Age=30, className=A, Name=John], Student [Age=24, className=A, Name=John], Student [Age=20, className=B, Name=John]]

提交回复
热议问题