分批次处理List

因为调用第三方接口,传入的参数有数量的限制要求。所以需要对传入的参数进行数量上的处理。
附上方法代码:
/**
* 通过subList来处理
* 
* 2017年10月27日上午2:06:24
* @param sourList准备处理的数据List<Object>
* @param batchCount准备以多少数目去处理,比如20,就是20一批处理
* @parameter
* void
*
*/
    public static void dealBySubList(List<Object> sourList, int batchCount){
        int sourListSize = sourList.size();
        int subCount = sourListSize%batchCount==0 ? sourListSize/batchCount : sourListSize/batchCount+1;
        int startIndext = 0;
        int stopIndext = 0;
        if(sourListSize >= batchCount){
        stopIndext = batchCount;
        }else{
        stopIndext = sourListSize;
        }
        for(int i=0;i<subCount;i++){
            List<Object> tempList = new ArrayList<Object>(sourList.subList(startIndext, stopIndext)); 
            printList(tempList);
            startIndext = startIndext + batchCount;
            stopIndext = stopIndext + batchCount;
            if(i == subCount-2 && stopIndext != sourListSize){
            stopIndext = sourListSize;
            }
            
        }
    }
测试代码:
    public static void main(String[] args) {
        List<Object> list = new ArrayList<Object>();  
        for (int i = 0; i < 10; i++) {  
            list.add(String.valueOf(i));  
        }  
        long start = System.nanoTime();
        dealBySubList(list, 20);
        long end = System.nanoTime();
        System.out.println("The elapsed time :" + (end-start));
         
    }
sourList和batchCount的关系: 
1,sourList = batchCount
2,sourList > batchCount
3,sourList < batchCount
经过测试:运行没有问题,对这个方法修改就能变成自己需要的方法,本质上是分页的思想。



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值