按批次处理list数据的两种方法

主要应用于list存储数据过多,不能使list整体进行其余操作

Java | 复制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package net.xsoftlab.na;
import java.util.ArrayList;
import java.util.List;
/**
 * 按批次处理list数据的两种方法
 * 主要应用于list存储数据过多,不能使list整体进行其余操作
 * @author zhouhongna
 * @date 2014-10-20
 *
 */
public class DealListByBatch {
     
    /**
     * 通过list的     subList(int fromIndex, int toIndex)方法实现
     * @param sourList 源list
     * @param batchCount 分组条数
     */
    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;
        for(int i=0;i<subCount;i++){
            stopIndext = (i==subCount-1) ? stopIndext + sourListSize%batchCount : stopIndext + batchCount;
            List<Object> tempList = new ArrayList<Object>(sourList.subList(startIndext, stopIndext)); 
            printList(tempList);
            startIndext = stopIndext;
        }
    }
     
    /**
     * 通过源list数据的逐条转移实现
     * @param sourList 源list
     * @param batchCount 分组条数
     */
    public static void dealByRemove(List<Object> sourList, int batchCount){
        List<Object> tempList = new ArrayList<Object>();
        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<Object> sourList){
        for(int j=0;j<sourList.size();j++){
            System.out.println(sourList.get(j));
        }
        System.out.println("------------------------");
    }
     
    /**
     * 测试主方法
     * @param args
     */
    public static void main(String[] args) {
        List<Object> list = new ArrayList<Object>();  
        for (int i = 0; i < 91; i++) {  
            list.add(i);  
        }  
        long start = System.nanoTime();
        dealBySubList(list, 10);
        dealByRemove(list, 10);
        long end = System.nanoTime();
        System.out.println("The elapsed time :" + (end-start));
         
    }
}

最新文章

  1. Android中使用ShareSDK集成分享功能
  2. ElasticSearch大数据分布式弹性搜索引擎使用
  3. Java 8之二小坑:stream parallel 和 lamada
  4. Python.with.context-manager
  5. WIN7 共享网络方法
  6. 基于nodejs实现js后端化处理
  7. MySQL 索引详解大全
  8. HDU 4612 Warm up tarjan缩环+求最长链
  9. Android JNI学习之javah命令的正确使用(找了好半天才找到的,汉,网上好多说法都没用)
  10. EASYUI+MVC4通用权限管理平台--前言
  11. phpcms v9 打开网站特别慢 增加数据库缓存方法
  12. -bash: warning: setlocale: LC_CTYPE: cannot change locale (EN_US.UTF-8): No such file or directory
  13. 深入理解Android中View
  14. 简单的MVC与SQL Server Express LocalDB
  15. centos7之zabbix入门(一)
  16. ubuntu安装vncserver实现图形化访问
  17. 两个字符串 char* a, char* b,输出b在a中的位置次序。
  18. TensorFlow全新的数据读取方式:Dataset API入门教程
  19. oracle使用已有vid快速新建虚拟机
  20. Python 爬虫 学习一

热门文章

  1. C++ 基类指针和子类指针相互赋值
  2. ubuntu内部错误的解决办法
  3. 彻底清除Linux centos minerd木马 实战  跟redis的设置有关
  4. JAX-WS使用Handler实现简单的WebService权限验证
  5. NGINX + LUA实现复杂的控制
  6. 【MySql】Java 批量插入数据库addBatch
  7. python跳一跳辅助学习
  8. oracle中空值null的判断和转换:NVL的用法
  9. 大数据(11) - kafka的安装与使用
  10. 程序中判断android系统版本