public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(6); list.add(7); list.add(8); list.add(9); list.add(10); list.add(11); //每个list存入条数 int maxSize = 5; //计算切分次数 int limit = (list.size() + maxSize - 1) / maxSize; List<List<Integer>> avgList = new ArrayList<>(); //等份切割,一般是skip与limit使用,maxSize = 5,表示5条为一个list,有11条数据,会分割成3个list Stream.iterate(0, n -> n + 1).limit(limit).forEach(i -> { //skip就是舍弃stream前多少个元素,limit就是返回流前面多少个元素(如果流里元素少于该值,则返回全部) //如skip((long)i * maxSize),i=0时,就是舍弃0个元素,i=1时,舍弃前面的5个元素,依次类推 List<Integer> tmpList = list.stream().skip((long)i * maxSize).limit(maxSize).collect(Collectors.toList()); avgList.add(tmpList); }); for (List<Integer> arr : avgList) { System.out.println(arr.toString()); } }
运行结果如下:
标签:等份,jdk,skip,list,maxSize,add,limit,List From: https://www.cnblogs.com/chunyansong/p/16985577.html