title: How to handle joining multiple asynchronous CompletableFutures?
body: <p>I've read that <code>CompletableFuture</code> has the ability to merge multiple futures with the <code>runAfterBoth</code> but what if I want to merge more than two?</p>
  
  <pre><code>CompletableFuture&lt;Boolean&gt; a = new CompletableFuture&lt;&gt;();
  CompletableFuture&lt;Boolean&gt; b = new CompletableFuture&lt;&gt;();
  CompletableFuture&lt;Boolean&gt; c = new CompletableFuture&lt;&gt;();
  
  List&lt;CompletableFuture&lt;Boolean&gt;&gt; list = new LinkedList&lt;&gt;();
  
  list.add(a);
  list.add(b);
  list.add(c);
  
  // Could be any number
  for (CompletableFuture&lt;Boolean&gt; f : list) {
     f.runAfter..
  }
  </code></pre>
  
  <p>My use case is that I'm sending messages out to multiple sockets to locate a single object which may or may not be on any one of them.</p>
  
  <p>I'm currently looking at this as a solution:</p>
  
  <pre><code>CompletableFuture&lt;Boolean&gt; a = new CompletableFuture&lt;&gt;();
  CompletableFuture&lt;Boolean&gt; b = new CompletableFuture&lt;&gt;();
  CompletableFuture&lt;Boolean&gt; c = new CompletableFuture&lt;&gt;();
  
  List&lt;CompletableFuture&lt;Boolean&gt;&gt; list = new LinkedList&lt;&gt;();
  
  list.add(a);
  list.add(b);
  list.add(c);
  
  CompletableFuture&lt;Boolean&gt; result = new CompletableFuture&lt;&gt;();
  
  Thread accept = new Thread(() -&gt; {
     for (CompletableFuture&lt;Boolean&gt; f : list)
        if (f.join() != null)
           result.complete(f.join());
  });
  
  accept.start();
  
  // Actual boolean value returned
  result.get();
  </code></pre>
  
  <p>But it's kind of a mess. And in my case, I want to continue processing as soon as I get a valid result (not null) instead of waiting on the invalid results as well.</p>
  
  <p>For example, <code>a</code> takes 5 seconds and the loop is waiting on it even though <code>b</code> has already completed in 2 seconds; but the loop doesn't know that because it's still waiting on <code>a</code>.</p>
  
  <p>Is there a pattern to work with joining multiple asynchronous futures where I can respond immediately on a successful completion?</p>
  
  <p>Another possibility:</p>
  
  <pre><code>public static class FutureUtil {
  public static &lt;T&gt; CompletableFuture&lt;T&gt; anyOfNot(
     Collection&lt;CompletableFuture&lt;T&gt;&gt; collection,
     T value,
     T defaultValue)
  {
     CompletableFuture&lt;T&gt; result = new CompletableFuture&lt;&gt;();
  
     new Thread(() -&gt; {
        for (CompletableFuture&lt;T&gt; f : collection) {
           f.thenAccept((
              T r) -&gt; {
              if ((r != null &amp;&amp; !r.equals(value))
                 || (value != null &amp;&amp; !value.equals(r)))
                 result.complete(r);
           });
        }
  
        try {
           for (CompletableFuture&lt;T&gt; f : collection)
              f.get();
        }
        catch (Exception ex) {
           result.completeExceptionally(ex);
        }
  
        result.complete(defaultValue);
     }).start();
  
     return result;
  }
  }
  </code></pre>
  
  <p>Example use:</p>
  
  <pre><code>CompletableFuture&lt;Boolean&gt; a = new CompletableFuture&lt;&gt;();
  CompletableFuture&lt;Boolean&gt; b = new CompletableFuture&lt;&gt;();
  CompletableFuture&lt;Boolean&gt; c = new CompletableFuture&lt;&gt;();
  
  List&lt;CompletableFuture&lt;Boolean&gt;&gt; list = new LinkedList&lt;&gt;();
  
  list.add(a);
  list.add(b);
  list.add(c);
  
  CompletableFuture&lt;Boolean&gt; result = FutureUtil.anyOfNot(list, null, false);
  
  result.get();
  </code></pre>