首页 > 解决方案 > 如何使用流 API 将数组划分为子数组

问题描述

我有一个大小为 1000 的数组。我想使用流操作来执行如下操作:-

List list= new ArrayList();
//list is initialize to 1000 elements 

  List subList = list.subList(0, 100);
   // perform some operaions over the subarray
  List subList1 = list.subList(101, 200);
   // perform some operaions over the subarray
 .... so on
}

我想要使​​用流 API 的代码。提前致谢

标签: javalistjava-8java-stream

解决方案


关于什么 :

  List<List<Integer>> result = IntStream.range(0, list.size() / 100)
         .mapToObj(index -> list.subList(index * 100, index * 100 + 100))
         .collect(Collectors.toList());

推荐阅读