List 分批处理

本文介绍了如何使用GoogleGuava、ApacheCommons和Hutool库在Java中进行列表分割,展示了如何利用partition方法将列表拆分成指定大小的子列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Google Guava

<dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>31.0.1-jre</version>
 </dependency>

List<String> tempList = Arrays.asList("水星","金星","地球","火星",
"冥王星","土星","天王星","海王星","冥王星","木星");
// size 是把集合拆分的大小,size 为表示拆分成拆分的集合大小为3,
// 后面不足3的有多少算多少
List<List<String>> partition = Lists.partition(tempList, 3);
System.out.println(partition);

[[水星, 金星, 地球], 
[火星, 冥王星, 土星], 
[天王星, 海王星, 冥王星], 
[木星]]

2.apache commons

<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.4</version>
        </dependency>

List<String> tempList = Arrays.asList("水星","金星","地球","火星","冥王星","土星","天王星","海王星","冥王星","木星");
List<List<String>> partition = ListUtils.partition(tempList, 6);
 
System.out.println(partition);

[[水星, 金星, 地球, 火星, 冥王星, 土星],
 [天王星, 海王星, 冥王星, 木星]]

3.Hutool

<dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.14</version>
        </dependency>
List<String> tempList = Arrays.asList("水星","金星","地球","火星","冥王星","土星","天王星","海王星","冥王星","木星");
 List<List<String>> partition = ListUtil.partition(tempList, 5);
 
 System.out.println(partition);

[[水星, 金星, 地球, 火星, 冥王星], 
[土星, 天王星, 海王星, 冥王星, 木星]]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值