是否有来自List< CompletionStage>的转换器?到CompletionStage< List>在Java中?
问题描述:
就像在使用 of
的假设示例中一样:
Like in this hypothetical example of using of
:
List<CompletionStage<Long>> listOfFutureLongs = getFutureLongs(...)
CompletionStage<List<Long>> futureListOfLongs = CompletionStage.of(listOfFutureLongs)
答
Internet说,使用可完成的将来:
Internet says, use completable future:
List<CompletionStage<Long>> futureLongs = getFutureLongs();
var arr = futureLongs.toArray(new CompletableFuture[futureLongs.size()]);
CompletionStage<List<Long>> result = CompletableFuture.allOf(arr)
.thenApply(unused -> futureLongs.stream().map(f -> f.toCompletableFuture().join()).collect(Collectors.toList()));