很多时候都有将List拆分为给定大小的多个子列表的操作,以前需要写很多复杂的逻辑才能实现,试试
强大的集合工具Iterables
Guava提供了一些java.util.Collections中没有提供的关于Iterable的公共操作,
而这些操作都封装在了工具类 - Iterables中.
List<Integer> list1 = Lists.newArrayListWithCapacity(0);
list1.add(null);
list1.add(1);
list1.add(2);
list1.add(9);
List<Integer> list2 = Lists.newArrayListWithCapacity(0);
list2.add(2);
list2.add(3);
list2.add(4);
list2.add(6);
合并方法 - concat()
Iterable<Integer> testIterable = Iterables.concat(list1, list2);
System.out.println(testIterable);//[null, 1, 2, 9, 2, 3, 4, 6]
分区方法 - partition()
Iterables提供了对指定Iterable<T>通过指定分区大小进行分区.并返回Iterable<List<T>>.
需要注意的是:Iterable不能为NULL并且指定的分区大小必须大于0.
Iterable<List<Integer>> partition = Iterables.partition(testIterable, 3);
System.out.println(partition);//[[null, 1, 2], [9, 2, 3], [4, 6]]