Lists
需要实例化一个
List<Person> personList = Lists.newArrayList();
public static <E> ArrayList<E> newArrayList(E... elements) { checkNotNull(elements); // for GWT // Avoid integer overflow when a large array is passed in int capacity = computeArrayListCapacity(elements.length); ArrayList<E> list = new ArrayList<>(capacity); Collections.addAll(list, elements); return list; }
官方文档中介绍,partation这个函数挺有趣的,可以分割成几几个List
List<Person> personList =
Lists.newArrayList(person1,person2,person3,person4);
Then we call the Lists.partition() method specifying two partitions:
List<List<Person>> subList = Lists.partition(personList,2);
In this example, the subList list would contain [[person1,person2],[person3,
person4]] . The partition method returns consecutive sublists of the same size,
with the exception of the last sublist, which may be smaller. For example, if 3 were
passed in as the size for the sublist method, Lists.partition() would have
returned [[person1,person2,person3],[person4]] .
实现
public static <T> List<List<T>> partition(List<T> list, int size) { checkNotNull(list); checkArgument(size > 0); return (list instanceof RandomAccess) ? new RandomAccessPartition<>(list, size) : new Partition<>(list, size); }
实现
final List<T> list; final int size;
Partition(List<T> list, int size) { this.list = list; this.size = size; }