如何迭代一个大列表以使其更小以使用 Java 流进行 REST 调用?

2022-01-22 00:00:00 java java-stream

我有类似这样的逻辑要分块处理.

I have logic something like this to process in chunks.

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 流进行迭代?

Is there a better way of doing this iteration using java streams?

推荐答案

你可以做类似 this answer, by使用步进创建范围,然后 subList:

You can do similar to this answer, by creating range with stepping and then subList:

int step = 100;
IntStream
  .iterate(0, o -> o < bigList.size(), o -> o + step)
  .mapToObj(i -> bigList.subList(i, Math.min(i + step, bigList.size()))
  .forEach(subList -> callRestApi(subList));

或者你可以提取方法:

private static <T>  Stream<List<T>> partition(List<T> list, int step) {
    return IntStream
        .iterate(0, o -> o < list.size(), o -> o + step)
        .mapToObj(i -> list
            .subList(i, Math.min(i + step, list.size()))
        );
}

然后

partition(bigList, 100).forEach(subList -> callRestApi(subList));

相关文章