I need to create a generic array of CompletableFuture
objects so that I can pass it to the CompletableFuture.allOf
method to get one CompletableFuture
to sync the threads. But since it is a generic I cannot create it. One obvious solution is to create a List and then call toArray
on it but it'll be inefficient. Are there any better methods? Here is my code:
// Current solution:
List<CompletableFuture<List<ReportComparable>>> newReports = new ArrayList<>();
// Loop and add CompletableFuture objects to this list
// Collect all the retrieved objects here(Sync Threads).
try {
List<List<ReportComparable>> newReps = CompletableFuture.allOf((CompletableFuture<?>[]) newReports.toArray()).get();
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
There are several wrong assumptions in your question
You can’t do
(CompletableFuture<?>[]) newReports.toArray()
. The parameterlesstoArray()
method will returnObject[]
and the casting attempt will cause aClassCastException
. Only thetoArray(T[])
method accepting an existing array will return an array of the same type, which brings you back to square zeroCompletableFuture.allOf
returns aCompletableFuture<Void>
, so you can’t callget()
on it and expect to get aList<List<ReportComparable>>
. You have to assemble the result list yourself after completion.There is no problem creating an array of a generic class, when all type arguments are wildcards. So
CompletableFuture<?>[] array = new CompletableFuture<?>[100];
works.
When you have an arbitrary number of elements, adding them to a
List
and invokingtoArray
on it, isn’t necessarily inefficient. The alternative, dealing with an array and an index manually, is doing the same as anArrayList
, but error prone. The single copying step oftoArray
is rarely performance relevant.Also keep in mind, since the
CompletableFuture.allOf
returnsCompletableFuture<Void>
you might need theList<CompletableFuture<List<ReportComparable>>>
anyway to be able to construct the desiredList<List<ReportComparable>>
after the completion.On the other hand, when you have a fixed number of arguments, you may call the varargs method
CompletableFuture.allOf
directly without manual array creation.
But when all you want to do with the CompletableFuture
returned by allOf
, is to call get()
immediately, the “wait for all” operation doesn’t have any benefit anyway.
You get the same effect implicitly when querying the individual CompletableFuture
instances via get()
or join()
and adding the result to your resulting List
, which you have to do anyway after completion, as CompletableFuture.allOf
does not do that for you.