Sorting by property in Java 8 stream

后端 未结 1 1623
挽巷
挽巷 2020-12-05 13:08

Oh, those tricky Java 8 streams with lambdas. They are very powerful, yet the intricacies take a bit to wrap one\'s header around it all.

Let\'s say I have a U

相关标签:
1条回答
  • 2020-12-05 13:37

    What you want is Comparator#comparing:

    userMap.values().stream()
        .sorted(Comparator.comparing(User::getName, UserNameComparator.INSTANCE))
        .collect(Collectors.toList());
    

    For the second part of your question, you would just use

    Comparator.comparing(
        u->u.getProfile().getUsername(), 
        UserNameComparator.INSTANCE
    )
    
    0 讨论(0)
提交回复
热议问题