lamba表达式方式对list结果集进行分页

1.使用java8,lamba表达式方式对list集合进行分页,不适用List<Map>类型的,如图:

@Test
    public void testPageList() {
        UserEntity entity = new UserEntity();
        UserEntity entity2 = new UserEntity();
        UserEntity entity3 = new UserEntity();
        UserEntity entity4 = new UserEntity();
        entity.setBaseId("a");
        entity2.setBaseId("c");
        entity3.setBaseId("e");
        entity4.setBaseId("b");
        //原始集合
        List<UserEntity> resultData = Arrays.asList(entity3,entity4,entity,entity2);
        //分页后的集合
        List<UserEntity> pageList = Lists.newArrayList();
        int currentPage=2,pageSize=2;
        if(!ObjectUtils.isEmpty(resultData)){
            //对原始集合按照baseId进行升序排序
            resultData.sort(Comparator.comparing( entityTmpl -> entityTmpl.getBaseId()));
            System.out.println("根据baseId排序后:"+JSON.toJSONString(resultData));
            //对list结果集进行分页
            pageList = resultData.stream().skip((currentPage-1)*pageSize)
                    .limit(pageSize).collect(Collectors.toList());
        }
        System.out.println("共"+resultData.size()+"条,每页"+pageSize+"条,当前第"+currentPage+"页的数据是:\n"+JSON.toJSONString(pageList));
    }

输出结果如下

根据baseId排序后:[{"baseId":"a"},{"baseId":"b"},{"baseId":"c"},{"baseId":"e"}]
共4条,每页2条,当前第2页的数据是:
[{"baseId":"c"},{"baseId":"e"}]

2.使用普通方式对list进行分页,为了处理list<Map<String,Object>>的list,如图:

@Test
    public void testPageList(){
        Map<String, Object> colormap = Maps.newHashMap();
        colormap.put("id","1");
        colormap.put("age","40");
        Map<String, Object> colormap1 = Maps.newHashMap();
        colormap1.put("id","2");
        colormap1.put("age","20");
        Map<String, Object> colormap2 = Maps.newHashMap();
        colormap2.put("id","3");
        colormap2.put("age","30");
        Map<String, Object> colormap3 = Maps.newHashMap();
        colormap3.put("id","4");
        colormap3.put("age","50");
        List<Map> resultData = Arrays.asList(colormap, colormap1, colormap2,colormap3);

        if (resultData != null && resultData.size() > 0) {
            Collections.sort(resultData, (o1, o2) -> Integer.parseInt((String)o1.get("age"))-Integer.parseInt((String)o2.get("age")));
            System.out.println("根据age排序后:"+JSON.toJSONString(resultData));

            List<Map> pageList = com.google.common.collect.Lists.newArrayList();
            int currentPage=2,pageSize=2;
            int currIdx = currentPage > 1 ? (currentPage - 1) * pageSize : 0;
            //循环截取某页列表进行分页
            for (int i = 0; i < pageSize && i < resultData.size() - currIdx; i++) {
                Map data = resultData.get(currIdx +  i);
                pageList.add(data);
            }
            System.out.println("共"+resultData.size()+"条,每页"+pageSize+"条,当前第"+currentPage+"页的数据是:\n"+JSON.toJSONString(pageList));
        }
    }

根据age排序后:[{"id":"2","age":"20"},{"id":"3","age":"30"},{"id":"1","age":"40"},{"id":"4","age":"50"}]
共4条,每页2条,当前第2页的数据是:
[{"id":"1","age":"40"},{"id":"4","age":"50"}] 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值