List 拆分(分批)处理

原创
2020/10/15 18:22
阅读数 1.9K
public class ListTest {
    public static void main(String[] args) {
        int total = 10000000;
        List<String> totalList = new ArrayList<>(total);
        for(int i = 0; i < total; i++){
            totalList.add("" + i);
        }
        int count = 500;
        int beginIndex = 0;
        int endIndex = beginIndex + count;
        while (beginIndex <= totalList.size() - 1){
            endIndex = endIndex <= totalList.size() ? endIndex : totalList.size();
            // 左闭右开区间[beginIndex, endIndex)
            List<String> subList = totalList.subList(beginIndex, endIndex);
            // 业务处理逻辑begin
            System.out.println("==============>endIndex:" + endIndex);
            System.out.println(subList);
            // 业务处理逻辑end
            beginIndex = endIndex;
            endIndex = beginIndex + count;
        }
    }
}

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部