There are several wrong assumptions in your question
You can’t do (CompletableFuture<?>[]) newReports.toArray()
. The parameterless toArray()
method will return Object[]
and the casting attempt will cause a ClassCastException
. Only the toArray(T[])
method accepting an existing array will return an array of the same type, which brings you back to square zero
CompletableFuture.allOf
returns a CompletableFuture<Void>
, so you can’t call get()
on it and expect to get a List<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 invoking toArray
on it, isn’t necessarily inefficient. The alternative, dealing with an array and an index manually, is doing the same as an ArrayList
, but error prone. The single copying step of toArray
is rarely performance relevant.
Also keep in mind, since the CompletableFuture.allOf
returns CompletableFuture<Void>
you might need the List<CompletableFuture<List<ReportComparable>>>
anyway to be able to construct the desired List<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.