Skip to content

Instantly share code, notes, and snippets.

@abner
Last active January 18, 2018 01:46
Show Gist options
  • Save abner/247e640d0360e8b717e5e47385d046ba to your computer and use it in GitHub Desktop.
Save abner/247e640d0360e8b717e5e47385d046ba to your computer and use it in GitHub Desktop.
Java - From Array Observable into a Array mapped from multiple Single operations
import io.vertx.core.json.JsonArray;
import rx.Observable;
import rx.Single;
/**
* @author abner2
*/
public class RxPlayground {
public static void main(String[] args) {
JsonArray jsonArray = new JsonArray().add(1).add(2).add(3);
// toList => See http://tomstechnicalblog.blogspot.com.br/2015/11/rxjava-operators-tolist.html
Object b = Observable
.from(jsonArray.getList().toArray())
.switchMap(x ->
doSome((Integer) x).toObservable()
)
.toList()
.toSingle()
;
((Single) b).subscribe(System.out::println);
System.out.println(b);
// Here, using Buffer operator
Object c = Observable
.from(jsonArray.getList().toArray())
.switchMap(x ->
doSome((Integer) x).toObservable()
)
.buffer(jsonArray.size())
.toSingle()
;
((Single) c).subscribe(System.out::println);
System.out.println(c);
}
public static Single<String> doSome(Integer ind) {
return Single.just(new Integer(ind + 1).toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment