list 数之拆分工具类

package com.rongfeng.bigdata.crm.modules.crm.crmoutside.util;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liangyongpeng
 * @create 2022/1/7 10:32
 */
public class SplitListUtil<T> {

    /**
     * 将originalList中的数据拆分为多个list。
     *
     * @param originalList 数据源
     * @param count 每个集合的总量
     * @return 返回拆分后的集合集
     */
    public List<List<T>> getGroupingList(List<T> originalList, int count ){

        List<List<T>> batchList = new ArrayList<>();

        // 开始下标
        int startIndex = 0;

        // 结束下标
        int lastIndex = 0;

        if(originalList.size()<count){
            batchList.add(originalList);
        }else{

            // 取商
            int floor = (int)Math.floor(originalList.size() / count);

            // 取模
            int mod = originalList.size()%count;

            for (int i = 0; i < floor; i++) {

                lastIndex = lastIndex + count;

                List<T>  cutList = originalList.subList(startIndex,lastIndex);

                batchList.add(cutList);

                startIndex = startIndex + count;

            }

            // 表示还有最后的一些数据
            if(mod > 0){

                List<T> lastList =  originalList.subList(lastIndex,originalList.size());

                batchList.add(lastList);

            }

        }

        return batchList;

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值