1、使用Lists.partition进行分割
// 建立一个list
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 12, 14, 64, 674, 6, 43, 43);
// 每4个元素分割为一个list
List<List<Integer>> partition = Lists.partition(integers, 4);
// 遍历每个list
partition.forEach(list->{
System.out.println(list.toString()+" 数量"+list.size());
});
输出结果:
[1, 2, 3, 4] 数量4
[5, 6, 7, 8] 数量4
[9, 11, 12, 12] 数量4
[14, 64, 674, 6] 数量4
[43, 43] 数量2
使用Lists.partition进行分割是比较简洁的写法,不需要自己去定义多个list进行操作。