您当前的位置: 首页 > 

星夜孤帆

暂无认证

  • 2浏览

    0关注

    626博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

利用Stream给集合排序

星夜孤帆 发布时间:2021-04-08 15:04:46 ,浏览量:2

public class StreamSortTest {

    @Data
    @AllArgsConstructor
    static class OrgAccountVo {
        private String userId;
        private String accountRole;
        private Date joinTime;
    }

    public static void main(String[] args) {
        List accountVoList = new ArrayList();
        accountVoList.add(new OrgAccountVo("5","管理员", new Date()));
        accountVoList.add(new OrgAccountVo("3","成员", new Date()));
        accountVoList.add(new OrgAccountVo("1","成员", new Date()));
        accountVoList.add(new OrgAccountVo("4","成员", new Date()));
        accountVoList.add(new OrgAccountVo("2","成员", new Date()));

        System.out.println("**************************排序前**************************");
        accountVoList.stream().forEach(vo -> System.out.println(vo));

        System.out.println("**************************按照userId排序**************************");
        List sortedAccountVoList = accountVoList.stream().sorted(Comparator.comparing(OrgAccountVo::getUserId)).collect(Collectors.toList());
        sortedAccountVoList.stream().forEach(vo -> System.out.println(vo));

        System.out.println("**************************按照成员排序**************************");
        List accountVoList1 = accountVoList.stream().sorted(Comparator.comparing(OrgAccountVo::getAccountRole)).collect(Collectors.toList());
        accountVoList1.stream().forEach(vo -> System.out.println(vo));

        System.out.println("**************************按照成员和时间排序**************************");
        List accountVoList2 = accountVoList.stream().sorted(Comparator.comparing(OrgAccountVo::getAccountRole).reversed()
                .thenComparing(OrgAccountVo::getJoinTime)).collect(Collectors.toList());
        accountVoList2.stream().forEach(vo -> System.out.println(vo));

    }
}

参考链接

关注
打赏
1636984416
查看更多评论
立即登录/注册

微信扫码登录

0.0368s