title: Return a CompletableFuture containing a list of CompletableFutures
body: <p>I'm trying to make the calls to multiple APIs faster.</p>
  
  <p>In the code below, <code>getFilteredEvents</code> is the currently synchronous version. I have the feeling that the <code>map(x -&gt; x.getFilteredEvents(eventResearch))</code> operation will wait on the response of each API (it uses <code>RestTemplate.exchange()</code> internally) before passing onto the next one to build the <code>List&lt;Event&gt;</code> that I want to return. A solution would probably be to launch the <code>map</code> call on separate threads, but I wanted to try out the <code>CompletableFuture</code> API.</p>
  
  <p>Thus, <code>getFilteredEventsFaster</code> is the result of my efforts to improve the response time.</p>
  
  <pre><code>@Service
  public class EventsResearchService {
  
      @Autowired
      private List&lt;UniformEventsResearchApi&gt; eventsResearchApis;
  
      // this works, but I'm trying to improve it
      public EventResearchResponse getFilteredEvents(EventResearch eventResearch) {
          List&lt;Event&gt; eventsList = eventsResearchApis
                  .stream()
                  .map(x -&gt; x.getFilteredEvents(eventResearch))
                  .flatMap(List::stream)
                  .collect(Collectors.toList());
  
          return extractResponse(eventResearch, eventsList);
      }
  
      // this doesn't work yet: what is wrong?
      public CompletableFuture&lt;List&lt;Event&gt;&gt; getFilteredEventsFaster(EventResearch eventResearch) {
          List&lt;CompletableFuture&lt;List&lt;Event&gt;&gt;&gt; futureEventsList = eventsResearchApis
                  .parallelStream()
                  .map(x -&gt; CompletableFuture.supplyAsync(() -&gt; x.getFilteredEvents(eventResearch)))
                  .collect(Collectors.toList());
  
          return CompletableFuture.allOf(futureEventsList.toArray(new CompletableFuture&lt;List&lt;Event&gt;&gt;[0]));
      }
  }
  </code></pre>
  
  <p>My understanding is that I would want to send a <code>CompletableFuture&lt;List&lt;Event&gt;&gt;</code> back to my frontend, rather than the <code>List&lt;CompletableFuture&lt;List&lt;Event&gt;&gt;&gt;</code>, hence the <code>CompletableFuture.allOf()</code> call (which, if I understood properly, resembles a <code>flatmap</code> operation, creating a <code>CompletableFuture</code> from multiple <code>CompleteableFuture</code>s).</p>
  
  <p>Unfortunately, as it is, I get a <code>Generic array creation</code> compilation error when using <code>new CompletableFuture&lt;List&lt;Event&gt;&gt;[0]</code>.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>I have the feeling that using the <code>join</code> method would indeed allow me to collect all the answers, but that would be a blocking operation on the thread of my Service, wouldn't it? (Which would defeat the purpose of trying to return a <code>CompletableFuture</code> to my frontend, if I understand correctly.)</p>