Collections

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值