按批次处理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));
         
    }
}

按批次处理list数据 (list按条数取)的更多相关文章

  1. 表单生成器(Form Builder)之mongodb表单数据查询——返回分页数据和总条数

    上一篇笔记将开始定义的存储结构处理了一下,将FormItems数组中的表单项都拿到mongodb document的最外层,和以前的关系型数据类似,之不过好多列都是动态的,不固定,不过这并没有什么影响 ...

  2. oracle 利用over 查询数据和总条数,一条sql搞定

    select count(*) over()总条数 ,a.*from table a

  3. Spark Steaming消费kafka数据条数变少问题

    对于基于Receiver 形式,我们可以通过配置 spark.streaming.receiver.maxRate 参数来限制每个 receiver 每秒最大可以接收的记录的数据:对于 Direct ...

  4. Oracle 查询库中所有表名、字段名、字段名说明,查询表的数据条数、表名、中文表名、

    查询所有表名:select t.table_name from user_tables t;查询所有字段名:select t.column_name from user_col_comments t; ...

  5. 实现java 中 list集合中有几十万条数据,每100条为一组取出

    解决"java 中 list集合中有几十万条数据,每100条为一组取出来如何实现,求代码!!!"的问题. 具体解决方案如下: /** * 实现java 中 list集合中有几十万条 ...

  6. jquery通过ajax获取数据,控制显示的数据条数

    效果图: 现在我们可以先看它的json数据,如图所示:                然后可以对应我们的代码进行理解. jquery通过ajax获取数据,并通过窗口大小控制显示的数据条数,以及可以根据 ...

  7. PROCEDURE_监测系统_数据备份存储过程—备份原始数据,每十分钟一条,取平均值

    create or replace procedure proc_backup_originaldata(retCode out varchar2, -- 返回码                    ...

  8. SQL 一列数据整合为一条数据

    SQL 一列数据整合为一条数据: SELECT  STUFF(( SELECT distinct  ',' + 列名 FROM 表名 where  [条件] FOR XML PATH('') ), 1 ...

  9. [lua, mysql] 将多条记录数据组合成一条sql插入语句(for mysql)

    -- 演示将多条记录数据组合成一条sql插入语句(for mysql) function getTpl0(tname) -- 获取表各个字段 local t = { tpl_pack = {" ...

随机推荐

  1. Atitit. 解决unterminated string literal 缺失引号

    Atitit. 解决unterminated string literal 缺失引号 原因:::或许string没使用引号括号起来...missingMessage缺失了一个单个的引号 Error:  ...

  2. MongoDB 学习 第八节 驱动实践

    作为系列的最后一篇,得要说说C#驱动对mongodb的操作,目前驱动有两种:官方驱动和samus驱动,不过我个人还是喜欢后者, 因为提供了丰富的linq操作,相当方便. 官方驱动:https://gi ...

  3. linux查看匹配内容的前后几行(转)

    linux系统中,利用grep打印匹配的上下几行   如果在只是想匹配模式的上下几行,grep可以实现.   $grep -5 'parttern' inputfile //打印匹配行的前后5行   ...

  4. CSS学习笔记(7)--html页面的CSS、DIV命名规则

    html页面的CSS.DIV命名规则 CSS命名规则 头:header 内容:content/containe 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整 ...

  5. Mac 终端编译运行 C++

    1.在编辑器中写好C++代码 2.打开终端打开文件对应的地址 3.用g++命令来编译.cpp文件 4.用./文件名来运行 观察文件的目录可发现 g++ 源文件名 编译源文件,产生a.out ./文件名 ...

  6. [mmc/sdio]Linux下的sdio和mmc

    http://www.cnblogs.com/RandyQ/p/3607107.html

  7. LAMP 环境搭建关键步骤及注意事项

    一.安装MySQL1): 编译安装MySQL+----------------------------------------------------------------------------- ...

  8. javascript jquery数组操作小结

    ----------------------------------------------------------定义数组-------------------------------------- ...

  9. C++ 运算符重载四(自定义数组类)

    //自定义数组类 #include<iostream> using namespace std; //分析:能获取数组长度,添加元素,删除元素,修改元素 //要求重载[],=,==,!=运 ...

  10. 第二百六十九节,Tornado框架-Session登录判断

    Tornado框架-Session登录判断 Session需要结合cookie来实现 Session的理解 1.用户登录系统时,服务器端获取系统当前时间,进行nd5加密,得到加密后的密串 2.将密串作 ...