List每次取100条进行处理

import java.util.ArrayList;
import java.util.List;
public class DealListByBatch {

    /**
     * 通过list的     subList(int fromIndex, int toIndex)方法实现
     * @param sourList 源list
     * @param batchCount 分组条数
     */
    public static void dealBySubList(List sourList, int batchCount){
        int sourListSize = sourList.size();
        int subCount = sourListSize%batchCount==0 ? sourListSize/batchCount : sourListSize/batchCount+1;
        int startIndext = 0;
        int stopIndext = 0;
        for(int i=0;i tempList = new ArrayList(sourList.subList(startIndext, stopIndext));
            printList(tempList);
            startIndext = stopIndext;
        }
    }

    /**
     * 通过源list数据的逐条转移实现
     * @param sourList 源list
     * @param batchCount 分组条数
     */
    public static void dealByRemove(List sourList, int batchCount){
        List tempList = new ArrayList();
        for (int i = 0; i < sourList.size(); i++) {
            tempList.add(sourList.get(i));
            if((i+1)%batchCount==0 || (i+1)==sourList.size()){
                printList(tempList);
                tempList.clear();
            }
        }
    }

    /**
     * 打印方法 充当list每批次数据的处理方法
     * @param sourList
     */
    public static void printList(List sourList){
        for(int j=0;j list = new ArrayList();
        for (int i = 0; i < 260; i++) {
            list.add(i);
        }
        long start = System.nanoTime();
        dealBySubList(list, 100 );
//        dealByRemove(list, 10);
        long end = System.nanoTime();
        System.out.println("The elapsed time :" + (end-start));

    }
} 
  

 

你可能感兴趣的:(java)