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

相关推荐
Spring AI学习17 小时前
Spring AI深度解析(9/50):可观测性与监控体系实战
java·人工智能·spring
java1234_小锋18 小时前
Spring IoC的实现机制是什么?
java·后端·spring
xqqxqxxq18 小时前
背单词软件技术笔记(V2.0扩展版)
java·笔记·python
消失的旧时光-194318 小时前
深入理解 Java 线程池(二):ThreadPoolExecutor 执行流程 + 运行状态 + ctl 原理全解析
java·开发语言
哈哈老师啊19 小时前
Springboot学生综合测评系统hxtne(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
java·数据库·spring boot
4311媒体网19 小时前
帝国cms调用文章内容 二开基本操作
java·开发语言·php
zwxu_19 小时前
Nginx NIO对比Java NIO
java·nginx·nio
可观测性用观测云20 小时前
Pyroscope Java 接入最佳实践
java
气π21 小时前
【JavaWeb】——(若依 + AI)-基础学习笔记
java·spring boot·笔记·学习·java-ee·mybatis·ruoyi
阿里云云原生21 小时前
AgentScope Java 1.0:从模型到应用,AI Agent 全生命周期管理利器!
java·云原生