如果需要比较多个参数,可以使用thenComparing方法来进行多重比较,示例如下:

users.stream()
     .sorted(Comparator.comparing(User::getQuestionNum)
                       .thenComparing(User::getCommentNum))
     .forEach(System.out::println);

这将首先按照问题数量进行排序,然后在问题数量相同的情况下,按照评论数量进行排序。

Java Stream排序:多字段比较(Comparator.thenComparing)

原文地址: https://www.cveoy.top/t/topic/ocG3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录