当前位置:网站首页>List分割最佳实践
List分割最佳实践
2022-04-22 05:34:00 【新猿一马】
目录
在工作中经常遇到需要将数组分割成多个子数组,然后进行批量处理的需求。那有没有比较优雅的实现呢?
经过多次实践,总结出 ListUtils.partition 和 Lists.partition 两种较好实现 。下面对这两种实现分别进行说明。
一 ListUtils.partition 方法
1.1 引入依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
1.2 代码演示
public static void main(String[] args) {
//初始化数组
List<Integer> parList = new ArrayList<>();
IntStream.range(0, 30).forEach(parList::add);
//分割成子数组
List<List<Integer>> subList = ListUtils.partition(parList, 10);
//遍历子数组
subList.forEach(list -> {
System.out.println(String.format("subList size:%s", list.size()));
System.out.println(String.format("list:%s", list.toString()));
});
}
1.3 输出结果

二 Lists.partition 方法
2.1 引入依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
2.2 代码演示
public static void main(String[] args) {
//初始化数组
List<Integer> parList = new ArrayList<>();
IntStream.range(0, 30).forEach(parList::add);
//分割成子数组
List<List<Integer>> subList = Lists.partition(parList, 10);
//遍历子数组
subList.forEach(list -> {
System.out.println(String.format("subList size:%s", list.size()));
System.out.println(String.format("list:%s", list.toString()));
});
}
2.3 输出结果

三 源码分析
3.1 ListUtils.partition 源码分析

最终 ListUtils.partition 调用 ListUtils.Partition 方法来处理。
ListUtils.Partition 源码如下:

Partition 类作为 ListUtils 静态内部类继承 AbstractList 类。重写了 get 和 size方法。
3.2 Lists.partition 源码分析

Lists.partition 方法最终会调用 new Partition<>(list, size)。
Partition 类源码如下:

Partition 类作为 Lists 静态内部类继承 AbstractList 类。重写了 get 、 size、isEmpty 方法。
四 性能对比
由于Lists.partition和ListUtils.partition底层实现都是通过Partition类来实现,性能差不多。
版权声明
本文为[新猿一马]所创,转载请带上原文链接,感谢
https://blog.csdn.net/jack1liu/article/details/122392796
边栏推荐
- Redis setting and obtaining expiration time
- list stream: reduce的使用实例
- IDEA的安裝与使用技巧
- Unreal engine sequence effect and audio binding trigger
- The signal isolator is used in the water treatment control system to solve the problem of limitation due to some on-site non electric installation conditions
- mysql中json类型字段用法
- 可重入锁线程等待唤醒示例
- 辰辰采草药
- 7.Domino piling
- CopyOnWriteArrayList的使用场景
猜你喜欢

为什么数组的下标都是从0开始而不是1?

提高工作效率的利器

MySQL Chapter 6 installation and use of Navicat

判断链表是否有环

Method for coexistence of Keil-C51 and keil arm

mysql中on duplicate key update 使用详解

Pratique du langage C (2) - - mise en oeuvre de l'addition polynomiale par liste liée

C language version: the pre order, middle order and post order non recursive traversal of binary tree

Strong connected component of "tarjan" undirected graph

C language practice (2) -- polynomial addition with linked list
随机推荐
Auto. JS canvas setting anti aliasing paint setAntiAlias(true);
判断链表是否有环
C language version: the establishment and basic operation of chain stack
C language version: establishment of circular linked list
Optional最佳实践
9.Life, the Universe, and Everything
MySQL command
C language version: the pre order, middle order and post order non recursive traversal of binary tree
redis设置与获取过期时间一网打尽
供应链服务术语
MySQL事务
数的范围( 二分 经典模板题目)
稳定性建设最佳实践
Obdumper common export commands
7.Domino piling
Linux下安装Mysql8
MySQL函数及练习题(二)
LockSupport.park和unpark,wait和notify
【转】MySQL:InnoDB一棵B+树可以存放多少行数据?
MySQL偶尔的卡顿