• java List分批处理


    java List分批处理,例如对List中的数据进行批量插入。

    方法一:

     1 /**
     2  * ClassName:Test List分批处理
     3  * @author Joe
     4  * @version
     5  * @since JDK 1.8
     6  */
     7 public class Test {
     8 
     9     public static void main(String[] args) throws InterruptedException {
    10         // 计数器
    11         int count = 1;
    12 
    13         List<String> list = new ArrayList<String>();
    14         // 填充List
    15         for(int i = 0; i<10003; i ++){
    16             list.add(i + "");
    17         }
    18         // 临时List
    19         List<String> tempList = new ArrayList<String>();
    20 
    21         for (String s : list) {
    22             tempList.add(s);
    23             // 分 1000条 执行 批量insert
    24             if (count == 1000) {
    25                 // 执行 insert 操作
    26                 System.out.println("insert-1-" + tempList);
    27                 // 重新计数
    28                 count = 1;
    29                 // 清空list,重新塞数据
    30                 tempList.clear();
    31             } else {
    32                 count++;
    33             }
    34         }
    35         
    36         // 扫尾
    37         if (count > 0) {
    38             // 执行 insert 操作
    39             System.out.println("insert-2-" + tempList);
    40             // 重新计数
    41             count = 1;
    42             // 清空list,重新塞数据
    43             tempList.clear();
    44         }
    45     }
    46 }

    方法二:

     1 /** 
     2  * ClassName:Test2  List分批处理
     3  * @author 原文地址:http://blog.csdn.net/lxxc11/article/details/52817817
     4  * @version
     5  * @since JDK 1.8
     6  */
     7 public class Test2 {
     8 
     9     public static void main(String[] args) {
    10         // 1.总记录数
    11         List<String> oldList = new ArrayList<String>();
    12         for (int i = 0; i < 10003; i++) {
    13             oldList.add(i + "");
    14         }
    15 
    16         // 2.分页数据信息
    17         int totalSize = oldList.size(); // 总记录数
    18         int pageSize = 1000; // 每页N条
    19         int totalPage = totalSize / pageSize; // 共N页
    20 
    21         if (totalSize % pageSize != 0) {
    22             totalPage += 1;
    23             if (totalSize < pageSize) {
    24                 pageSize = oldList.size();
    25             }
    26         }
    27         System.out.println("循环保存的次数:" + totalPage);    // 循环多少次
    28 
    29         // 临时List
    30         List<String> temList = null;
    31         for (int pageNum = 1; pageNum < totalPage + 1; pageNum++) {
    32             int starNum = (pageNum - 1) * pageSize;
    33             int endNum = pageNum * pageSize > totalSize ? (totalSize) : pageNum * pageSize;
    34             System.out.println("起始:" + starNum + "-" + endNum);
    35             temList = oldList.subList(starNum, endNum);
    36             System.out.println("第" + pageNum + "批,执行insert:" + temList);
    37         }
    38     }
    39 }
  • 相关阅读:
    Linux 命令汇总总结相关
    数据结构---python---表
    python-----Queue模块
    再看python多线程------threading模块
    <转> Struct 和 Union区别 以及 对内存对齐方式的说明
    python装饰器
    HTTP权威指南----缓存
    HTTP权威指南----连接管理
    以python理解Linux的IO多路复用,select、poll、epoll
    <转载> pycharm快捷键及一些常用设置
  • 原文地址:https://www.cnblogs.com/xbq8080/p/7365189.html
Copyright © 2020-2023  润新知