title: RxJava: how to compose multiple Observables with dependencies and collect all results at the end?
body: <p>I'm learning RxJava and, as my first experiment, trying to rewrite the code in the first <code>run()</code> method in <a href="https://gist.github.com/benjchristensen/4671081">this code</a> (cited on <a href="http://techblog.netflix.com/2013/02/rxjava-netflix-api.html">Netflix's blog</a> as a problem RxJava can help solve) to improve its asynchronicity using RxJava, i.e. so it doesn't wait for the result of the first Future (<code>f1.get()</code>) before proceeding on to the rest of the code.</p>
  
  <p><code>f3</code> depends on <code>f1</code>. I see how to handle this, <code>flatMap</code> seems to do the trick:</p>
  
  <pre><code>Observable&lt;String&gt; f3Observable = Observable.from(executor.submit(new CallToRemoteServiceA()))
      .flatMap(new Func1&lt;String, Observable&lt;String&gt;&gt;() {
          @Override
          public Observable&lt;String&gt; call(String s) {
              return Observable.from(executor.submit(new CallToRemoteServiceC(s)));
          }
      });
  </code></pre>
  
  <p>Next, <code>f4</code> and <code>f5</code> depend on <code>f2</code>. I have this:</p>
  
  <pre><code>final Observable&lt;Integer&gt; f4And5Observable = Observable.from(executor.submit(new CallToRemoteServiceB()))
      .flatMap(new Func1&lt;Integer, Observable&lt;Integer&gt;&gt;() {
          @Override
          public Observable&lt;Integer&gt; call(Integer i) {
              Observable&lt;Integer&gt; f4Observable = Observable.from(executor.submit(new CallToRemoteServiceD(i)));
              Observable&lt;Integer&gt; f5Observable = Observable.from(executor.submit(new CallToRemoteServiceE(i)));
              return Observable.merge(f4Observable, f5Observable);
          }
      });
  </code></pre>
  
  <p>Which starts to get weird (<code>merge</code>ing them probably isn't what I want...) but allows me to do this at the end, not quite what I want:</p>
  
  <pre><code>f3Observable.subscribe(new Action1&lt;String&gt;() {
      @Override
      public void call(String s) {
          System.out.println("Observed from f3: " + s);
          f4And5Observable.subscribe(new Action1&lt;Integer&gt;() {
              @Override
              public void call(Integer i) {
                  System.out.println("Observed from f4 and f5: " + i);
              }
          });
      }
  });
  </code></pre>
  
  <p>That gives me:</p>
  
  <pre><code>Observed from f3: responseB_responseA
  Observed from f4 and f5: 140
  Observed from f4 and f5: 5100
  </code></pre>
  
  <p>which is all the numbers, but unfortunately I get the results in separate invocations, so I can't quite replace the final println in the original code:</p>
  
  <pre><code>System.out.println(f3.get() + " =&gt; " + (f4.get() * f5.get()));
  </code></pre>
  
  <p>I don't understand how to get access to both those return values on the same line. I think there's probably some functional programming fu I'm missing here. How can I do this? Thanks.</p>