I have an input stream of queries which are getting executed asynchronously. I want to make sure that when I use Completablefuture::join
, the result of those requires are collected int the order of input query stream.
This is how my code looks like:
queries.stream()
.map(query -> CompletableFuture.supplyAsync(() -> {
try {
return SQLQueryEngine.execute(query);
} catch (InternalErrorException e) {
throw new RuntimeException(e);
}
}))
.map(CompletableFuture::join)
.collect(Collectors.toList());
SQLQueryEngine.execute(query); returns a List<Results>
so output is List<List<Result>
. I want to flatten and combine all the results into one single List. If i use .flatMap(List::stream) before collection to flatten, will it maintain the ordering?