Skip to content

Instantly share code, notes, and snippets.

@milanboers
Created May 17, 2017 10:04
Show Gist options
  • Save milanboers/85c164b6d35f70f7c39b6d5e3b6864b6 to your computer and use it in GitHub Desktop.
Save milanboers/85c164b6d35f70f7c39b6d5e3b6864b6 to your computer and use it in GitHub Desktop.
Convert a List of CompletableFutures to a CompletableFuture of a List
public static <T> CompletableFuture<List<T>> listOfFuturesToFutureList(final List<CompletableFuture<T>> listOfFutures) {
final Function<List<CompletableFuture<T>>, List<T>> transformFunction = futures -> futures.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList());
return CompletableFuture.allOf(listOfFutures.toArray(new CompletableFuture[0]))
.thenApply(v -> transformFunction.apply(listOfFutures));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment