将list集合按"指定长度"进行切分,返回新的List<List<类型>>集合,如下的:
方法1:List<List<Integer>> lists=Lists.partition(numList,3);
方法2:List<List<Integer>> partition =ListUtils.partition(numList, 3);
1 package com.bessky.pss.wzw;
2
3 import java.util.List;
4 import org.apache.commons.collections4.ListUtils;
5 import org.junit.Test;
6 import com.google.common.collect.Lists;
7
8 /**
9 * 测试类
10 *
11 * @author win10
12 * @date 2021/3/3
13 */
14 public class WzwTest
15 {
16 @Test
17 public void test()
18 {
19 // 创建并初始化List集合
20 List<Integer> numList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
21
22 // 将List集合按一个List长度为3个值的List进行切分,返回新的List<List<Integer>>集合
23 List<List<Integer>> lists = ListUtils.partition(numList, 3);
24 // 或者方法2,作用和上面的方法一样
25 List<List<Integer>> partition = Lists.partition(numList, 3);
26
27 // 打印
28 System.out.println(lists);// [[1, 2, 3], [4, 5, 6], [7, 8]]
29 System.out.println("partition = " + partition); // partition = [[1, 2, 3], [4, 5, 6], [7, 8]]
30 }
31 }
结果:[[1, 2, 3], [4, 5, 6], [7, 8]]
著作:王子威