Skip to content

Instantly share code, notes, and snippets.

@droxer
Created July 15, 2015 05:55
Show Gist options
  • Save droxer/838a74d7360ba3df016d to your computer and use it in GitHub Desktop.
Save droxer/838a74d7360ba3df016d to your computer and use it in GitHub Desktop.
RxJava example.
package rxexamples;
import rx.schedulers.Schedulers;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class App {
public static Observable<Integer> getIds(){
List<Integer> ts = Arrays.asList(1, 2);
return Observable.defer(() ->
Observable.from(ts)
).subscribeOn(Schedulers.io());
}
public static Observable<String> getNames(Integer id) {
Map<Integer, List<String>> names = new HashMap<>();
names.put(1, Arrays.asList("A", "B", "C"));
names.put(2, Arrays.asList("a", "b", "c"));
List<String> strings = names.get(id);
return Observable.defer(() ->
Observable.from(strings)
).subscribeOn(Schedulers.io());
}
public static Observable<String> getSummaries(String name){
Map<String, List<String>> summaries = new HashMap<>();
summaries.put("A", Arrays.asList("AA", "AAA"));
summaries.put("B", Arrays.asList("BB", "BBB"));
summaries.put("C", Arrays.asList("CC", "CCC"));
summaries.put("a", Arrays.asList("aa", "aaa"));
summaries.put("b", Arrays.asList("bb", "bbb"));
summaries.put("c", Arrays.asList("cc", "ccc"));
List<String> strings = summaries.get(name);
return Observable.defer(() ->
Observable.from(strings)
).subscribeOn(Schedulers.io());
}
public static void main(String[] args) throws IOException {
System.out.println("Start...");
getIds().flatMap(id -> getNames(id).flatMap(name -> getSummaries(name))).forEach(item -> {
System.out.println(Thread.currentThread() + "[Async] get " + item);
});
System.in.read();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment