completion-stage

Is there a converter from List<CompletionStage> to CompletionStage<List> in Java?

白昼怎懂夜的黑 提交于 2020-06-01 03:49:46
问题 Like in this hypothetical example of using of : List<CompletionStage<Long>> listOfFutureLongs = getFutureLongs(...) CompletionStage<List<Long>> futureListOfLongs = CompletionStage.of(listOfFutureLongs) 回答1: Strangely no. There's CompletableFuture.allOf for CompletableFuture , which is kind of like what you want, but no similar function for CompletionStage . You can either use CompletionStage.toCompletableFuture to get futures, or you can write your own. Unfortunately, the inability to check a

How to nicely do allOf/AnyOf with Collections of CompletionStage

依然范特西╮ 提交于 2019-12-12 18:27:00
问题 Currently to do something simple with Collections of CompletionStage requires jumping through several ugly hoops: public static CompletionStage<String> translate(String foo) { // just example code to reproduce return CompletableFuture.completedFuture("translated " + foo); } public static CompletionStage<List<String>> translateAllAsync(List<String> input) { List<CompletableFuture<String>> tFutures = input.stream() .map(s -> translate(s) .toCompletableFuture()) .collect(Collectors.toList()); //