Java开发中List数据量大,需要分片批次处理

在开发过程中可能会遇到需要处理的List数据量过大,可以选择分批处理的方式对大量数据进行处理。

1、使用 apache 的工具包

xml 复制代码
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.4</version>
</dependency>

代码示例:Lists.partition()

java 复制代码
	 List<Integer> list=new ArrayList<>();
	 for (int i=0;i<500;i++){
	     list.add(i);
	 }
	 List<List<Integer>> newList = Lists.partition(list, 150);
	 for (List<Integer> subset:newList){
	     System.out.println(subset.size());
	 }

2、使用 guava 的工具包

xml 复制代码
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.0.1-jre</version>
</dependency>

代码示例:Lists.partition()

java 复制代码
	 List<Integer> list=new ArrayList<>();
	 for (int i=0;i<500;i++){
	     list.add(i);
	 }
	 List<List<Integer>> newList = Lists.partition(list, 150);
	 for (List<Integer> subset:newList){
	     System.out.println(subset.size());
	 }

当然还有其他工具包也封装了List分批处理的函数。


参考链接:

https://blog.csdn.net/qq_35387940/article/details/121612391

相关推荐
还是鼠鼠8 分钟前
《黑马商城》微服务保护-详细介绍【简单易懂注释版】
java·spring boot·spring·spring cloud·sentinel·maven
她说..26 分钟前
通过git拉取前端项目
java·前端·git·vscode·拉取代码
青衫码上行42 分钟前
【从0开始学习Java | 第18篇】集合(下 - Map部分)
java·学习
我星期八休息1 小时前
C++异常处理全面解析:从基础到应用
java·开发语言·c++·人工智能·python·架构
江湖有缘1 小时前
【Docker项目实战】使用Docker部署ShowDoc文档管理工具
java·docker·容器
2401_841495641 小时前
【数据结构】汉诺塔问题
java·数据结构·c++·python·算法·递归·
程序猿阿越1 小时前
Kafka源码(六)消费者消费
java·后端·源码阅读
失散131 小时前
分布式专题——35 Netty的使用和常用组件辨析
java·分布式·架构·netty
Terio_my1 小时前
Spring Boot 热部署配置
java·spring boot·后端
xxxxxxllllllshi2 小时前
Java 集合框架全解析:从数据结构到源码实战
java·开发语言·数据结构·面试