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

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

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

usersstreamsortedComparatorcomparingUsergetQuestionNum+UsergetCommentNumcomparing过程需要变量多个参数和该怎么办

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

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