首页 > 解决方案 > 如何迭代一个大列表以使其更小以使用 Java 流进行 REST 调用?

问题描述

我有类似这样的逻辑来分块处理。

List<String> bigList = getList(); // Returns a big list of 1000 recs
int startIndex=0;
boolean entireListNotProcessed=false;
while(entireListNotProcessed) {
   int endIndex = startIndex + 100;
   if(endIndex > myList.size()-1) {
      endIndex=myList.size()-1;
   }
   List<String> subList= bigList.subList(startIndex, endIndex);
   //call Rest API with subList and update entireListNotProcessed flag...
}

有没有更好的方法来使用 java 流进行这个迭代?

标签: javajava-stream

解决方案


您可以使用apache commons中的 ListUtils 为您进行拆分(如果您真的需要,因为 1000 并不多)

List<List<Integer>> output = ListUtils.partition(largeList, targetSize);

推荐阅读