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