List分页切分

对于系统间数据的交互,都应该进行数据量的限制,那么就经常需要对项目中的数据集合进行分页处理然后进行接口交互,这里整理一个集合的分页处理代码,方便以后使用也希望能帮到大家

 	/**
     * 把集合数据按指定的单页条数拆分
     * */
    public static <T> List<List<T>> rationing(List<T> source, int n) {
        List<List<T>> result = new ArrayList<List<T>>();
        //设置初始下标为0,每次成长分页大小,当endIndex等于集合长度时因为有i < list.size()所以也不会出现溢出页
        for (int i = 0; i < source.size(); i += n) {
            //如果分页toIndex下标大于当前集合长度,那么以集合长度为分页toIndex下标
            int endIndex = i + n;
            if (endIndex > source.size()) {
                endIndex = source.size();
            }
            //获取到分页后的一页集合
            List<T> newList = source.subList(i,endIndex );
            result.add(newList);
        }
        return result;
    }
 	/**
     * 把集合数据按指定的总页数均分
     * */
    public static <T> List<List<T>> averageAssign(List<T> source, int n) {
        List<List<T>> result = new ArrayList<List<T>>();
        //(先计算出余数)
        int remainder = source.size() % n;
        //然后是商
        int number = source.size() / n;
        //偏移量
        int offset = 0;
        for (int i = 0; i < n; i++) {
            List<T> value = null;
            if (remainder > 0) {
                value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
                remainder--;
                offset++;
            } else {
                value = source.subList(i * number + offset, (i + 1) * number + offset);
            }
            result.add(value);
        }
        return result;
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值