Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class Main {
    public static <T> CompletableFuture<List<T>> allOfFutures(
            List<? extends CompletableFuture<? extends T>> futures) {
        List<T> result = new ArrayList<>(futures.size());
        CompletableFuture<List<T>> cf = new CompletableFuture<>();
        CompletableFuture
                .allOf(futures.stream().map(s -> s.thenAccept(result::add)).toArray(CompletableFuture<?>[]::new))
                .exceptionally(ex -> {
                    cf.completeExceptionally(ex);
                    return null;
                }).thenAccept(v -> cf.complete(result));
        return cf;
    }
}