Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save anitaa1990/7aef38776789bdccbcee179faf55ab04 to your computer and use it in GitHub Desktop.
Save anitaa1990/7aef38776789bdccbcee179faf55ab04 to your computer and use it in GitHub Desktop.
/*
* Step 1: Create an observable that emits an integer
* from 1 to 5. Each item is squared by itself before it is
* emitted.
* */
Observable<Integer> observable = Observable.range(1, 5)
.subscribeOn(Schedulers.io())
.map(integer -> {
System.out.println(String.format("Squaring %d with itself", integer));
return integer * integer;
});
/*
* Step 2: Create a subject that observes
* this emission from the observable.
* */
ReplaySubject<Integer> subject = ReplaySubject.create();
observable.subscribe(subject);
/*
* Step 3: We are subscribing two subscribers to the Subject.
* */
subject.subscribe(s -> System.out.println("subscriber one: " + s));
subject.subscribe(s -> System.out.println("subscriber two: " + s));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment