Exercise – Creating io.reactivex.Observable
Write code that demonstrates several ways to create io.reactivex.Observable
. In each example, subscribe to the created Observable
object and print the emitted values.
Note
We did not discuss this so you will need to study the RxJava2 API and look up examples on the internet.
Answer
Here are six of the methods that allow you to create io.reactivex.Observable
:
//1 Observable.just("Hi!").subscribe(System.out::println); //prints: Hi! //2 Observable.fromIterable(List.of("1","2","3")) .subscribe(System.out::print); //prints: 123 System.out.println(); //3 String[] arr = {"1","2","3"}; Observable.fromArray(arr).subscribe(System.out::print); //prints: 123 System.out.println(); //4 Observable.fromCallable(()->123) .subscribe(System.out::println); //prints: 123 //5 ExecutorService pool = Executors.newSingleThreadExecutor(); Future<String> future = pool .submit(() -> { Thread.sleep(100); return "Hi...