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

相关推荐
程序无bug6 分钟前
手写Spring框架
java·后端
程序无bug8 分钟前
Spring 面向切面编程AOP 详细讲解
java·前端
全干engineer19 分钟前
Spring Boot 实现主表+明细表 Excel 导出(EasyPOI 实战)
java·spring boot·后端·excel·easypoi·excel导出
Fireworkitte29 分钟前
Java 中导出包含多个 Sheet 的 Excel 文件
java·开发语言·excel
GodKeyNet1 小时前
设计模式-责任链模式
java·设计模式·责任链模式
a_Dragon11 小时前
Spring Boot多环境开发-Profiles
java·spring boot·后端·intellij-idea
泽02021 小时前
C++之红黑树认识与实现
java·c++·rpc
ChinaRainbowSea1 小时前
补充:问题:CORS ,前后端访问跨域问题
java·spring boot·后端·spring
KiddoStone1 小时前
多实例schedule job同步数据流的数据一致性设计和实现方案
java
岁忧2 小时前
(LeetCode 每日一题) 1865. 找出和为指定值的下标对 (哈希表)
java·c++·算法·leetcode·go·散列表