title: Combining to a Map of parallelly downloaded images with RxJava Observables
body: <p>I'm trying to do a parallel download of a list of images, combining them to a map.</p>
  
  <p>At first I tried to make an Observable like this:</p>
  
  <pre><code>Observable&lt;Map&lt;Integer, Bitmap&gt;&gt; getImages(final List&lt;Activity&gt; activities) {
      return Observable.create(new Observable.OnSubscribe&lt;Map&lt;Integer, Bitmap&gt;&gt;() {
          @Override
          public void call(Subscriber&lt;? super Map&lt;Integer, Bitmap&gt;&gt; subscriber) {
              try {
                  Map&lt;Integer, Bitmap&gt; result = new HashMap&lt;Integer, Bitmap&gt;();
                  for (Activity act : activities) {
                      result.put(act.getId(), downloadImage(act.getImage()));
                  }
                  subscriber.onNext(result);
                  subscriber.onCompleted();
              } catch (Exception e) {
                  subscriber.onError(e);
              }
          }
      });
  }
  </code></pre>
  
  <p>This works, but it's not what I want. Because the images are being downloaded sequentially. And I think for-loops are not nice in rxjava. So I created these Observables:</p>
  
  <pre><code>    Observable&lt;Bitmap&gt; getImage(final Activity activity) {
      return Observable.create(new Observable.OnSubscribe&lt;Bitmap&gt;() {
          @Override
          public void call(Subscriber&lt;? super Bitmap&gt; subscriber) {
              try {
                  subscriber.onNext(downloadImage(activity.getImage()));
                  subscriber.onCompleted();
              } catch (Exception e) {
                  subscriber.onError(e);
              }
          }
      });
  }
  
  Observable&lt;Map&lt;Integer, Bitmap&gt;&gt; getImages(final List&lt;Activity&gt; activities) {
      return Observable
          .from(activities)
          .flatMap(new Func1&lt;Activity, Observable&lt;Bitmap&gt;&gt;() {
              @Override
              public Observable&lt;Bitmap&gt; call(Activity activity) {
                  return getImage(activity);
              }
          })
          .toMap(new Func1&lt;Bitmap, Integer&gt;() {
              @Override
              public Integer call(Bitmap bitmap) {
                  return 1; // How am I supposed to get the activity.getId()?
              }
          });
  }
  </code></pre>
  
  <p>So I made an Observable for getting a single image, trying to combine them in the second one using flatMap. This works, but there are still 2 problems:</p>
  
  <ol>
  <li>When I do a toMap(), how can I retrieve the right id to use as key for the map? I want to use the Activity object for that.</li>
  <li>Unfortunately, the downloads are still beging processed sequentially and not in parallel. How can I fix this?</li>
  </ol>