How to chain output of one flatmap into another and retain the intial input?


I have a KeyList. Using
.flatMap(new Func1<Entry<String, String>, Observable>()
public Observable call(final Entry<String, Long> entry) {

I want the output as : Map<entry.getKey, bucket.async().get(entry.getKey()>


In this precise case it could be achievable by letting RxJava aggregate results into a Map, since the key is part of the Document returned by get. Just start from the list of keys:

Observable<Map<String, JsonDocument>> result = 
//start from the keys to fetch
        .flatMap(new Func1<String, Observable<JsonDocument>>() {
            public Observable<JsonDocument> call(String key) {
                //fetch each document
                return bucket.async().get(key);
        .toMap(new Func1<JsonDocument, String>() {
            public String call(JsonDocument jsonDocument) {
                //aggregate all into a map which keys are each document's id()
        }); //no second function => the values will be the whole documents

This gives you a Map<String, JsonDocument> (which would correspond to the pseudo signature you required).

If you wanted the content of the document as a JsonObject instead, you can use the alternative toMap that also takes a function to select the values, and return the document’s content().

If you want the JSON as a String, use the above tip + use get(key, RawJsonDocument.class) in the flatMap, its content() will be the String representation of the JSON.