How to sort the name along with age in java

后端 未结 8 1121
失恋的感觉
失恋的感觉 2021-01-17 23:34

I am new to Java 8. I just want to sort by the name. But the condition is: if there are duplicate names then it should be sorted according to age.

For example my inp

8条回答
  •  野的像风
    2021-01-17 23:55

    You can simply use the stream sorted method with static reference of getName and getAge methods.

    List list=new ArrayList();
    list.stream().sorted(Comparator.comparing(Employee::getName).thenComparing(Employee::getAge)).collect(Collectors.toList()).forEach(System.out::println);
    

提交回复
热议问题