I am trying to achieve something like this. This is a made up example that expresses the intent.
I want all the completable futures to execute and combine all their results to one result and return that. So for the example below, the collection allResults should have the strings "one", "two", "three", 3 times each. I want them all to run in parallel and not serially.
Any pointers to what API on the completable future I could use to achieve this, would be very helpful.
public class Main {
public static void main(String[] args) {
int x = 3;
List<String> allResuts;
for (int i = 0; i < x; i++) {
//call getCompletableFutureResult() and combine all the results
}
}
public static CompletableFuture<List<String>> getCompletableFutureResult() {
return CompletableFuture.supplyAsync(() -> getResult());
}
private static List<String> getResult() {
List<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("three");
return list;
}
}