Oracle环境下如果代码程序批量插入超过1000条数据,往往是不成的,代码中需要将相关List集合拆分控制在1000条数据以内。本篇记录List拆分的3种方式:
1.apache commons
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
引入commons 框架之后,只需要使用 ListUtils.partition 方法即可实现分片,如下代码所示:
List<String> tempList = Arrays.asList("水星","金星","地球","火星","冥王星","土星","天王星","海王星","冥王星","木星");
List<List<String>> partition = ListUtils.partition(tempList, 6);
System.out.println(partition);
拆分结果:
[[水星, 金星, 地球, 火星, 冥王星, 土星],
[天王星, 海王星, 冥王星, 木星]]
2.Google Guava
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
</dependency>
引入 Guava 框架之后,只需要使用 Lists.partition 方法即可实现分片,如下代码所示:
List<String> tempList = Arrays.asList("水星","金星","地球","火星",
"冥王星","土星","天王星","海王星","冥王星","木星");
List<List<String>> partition = Lists.partition(tempList, 3);
System.out.println(partition);
拆分结果:
[[水星, 金星, 地球],
[火星, 冥王星, 土星],
[天王星, 海王星, 冥王星],
[木星]]
3.Hutool工具类
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.14</version>
</dependency>
引入Hutool 框架之后,只需要使用 ListUtil.partition 方法即可实现分片,如下代码所示:
List<String> tempList = Arrays.asList("水星","金星","地球","火星","冥王星","土星","天王星","海王星","冥王星","木星");
List<List<String>> partition = ListUtil.partition(tempList, 5);
System.out.println(partition);
拆分结果:
[[水星, 金星, 地球, 火星, 冥王星],
[土星, 天王星, 海王星, 冥王星, 木星]]