牛骨文教育服务平台(让学习变的简单)
博文笔记

java List.subList()方法类似分页分批处理数据

创建时间:2015-07-22 投稿人: 浏览次数:990
List<Integer> list = new ArrayList<Integer>();
        for(int i = 0; i < 253; i++){
            list.add(i);
        }
        int size = 251;
        int batchNum = 50;  //单个退款批次号对应的退款笔数,默认为每次50笔
        int count = size % batchNum > 0 ? size / batchNum + 1 : size / batchNum;    //要发起请求的次数
        int fromIndex = 0;
        int toIndex = 0;

        System.out.println("count: " + count);

        for(int i = 0; i < count; i++){
            fromIndex = i * batchNum;
            if(i == count - 1 && (size % batchNum != 0)){
                batchNum = size % batchNum;
                toIndex = size;

            }else{
                toIndex = (i + 1) * batchNum ;
            }
            System.out.println("fromIndex: " + fromIndex + " toIndex: " + toIndex + " batchNum: " + batchNum +" i: " + (i+1));
            System.out.println(list.subList(fromIndex, toIndex));  
        }
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。