以上代码的执行结果如下图所示:
2.apache commons
先在项目的 pom.xml 中添加框架支持,增加以下配置:
org.apache.commons
commons-collections4
4.4
复制代码
有了 commons 框架之后,只需要使用 ListUtils.partition 方法即可实现分片,如下代码所示:
import org.apache.commons.collections4.ListUtils;
import java.util.Arrays;
import java.util.List;
/**
- commons.collections4 集合分片
*/
public class PartitionExample {
// 原集合
private static final List OLD_LIST = Arrays.asList(
“唐僧,悟空,八戒,沙僧,曹操,刘备,孙权”.split(“,”));
public static void main(String[] args) {
// 集合分片
List<List> newList = ListUtils.partition(OLD_LIST, 3);
newList.forEach(i -> {
System.out.printl