按批次处理list数据 (list按条数取)
按批次处理list数据的两种方法
主要应用于list存储数据过多,不能使list整体进行其余操作
|
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)); }} |
浙公网安备 33010602011771号