In ReactiveX programming we may need combine various observables.
Some times we may need to get two sets of asynchronous data streams that are independent of each other.
Instead of waiting for the previous stream to complete before requesting the next stream, we can call both at the same time and subscribe to the combined streams
Below are the operators which are used to create a single Observable from multiple Observable
Sr.No. |
Operator & Description |
1 |
And/Then/When
Combine item sets using Pattern and Plan intermediaries.
|
2 |
CombineLatest
Combine the latest item emitted by each Observable via a specified function and emit resulted item.
|
3 |
Join
Combine items emitted by two Observables if emitted during time-frame of second Observable emitted item.
|
4 |
Merge
Combines the items emitted of Observables.
|
5 |
StartWith
Emit a specified sequence of items before starting to emit the items from the source Observable
|
6 |
Switch
Emits the most recent items emitted by Observables.
|
7 |
Zip
Combines items of Observables based on function and emits the resulted items.
|
Examples
Merge operator to combine the output of multiple Observables
public class RxCombine {
public static void main(String args[])
{
Observable.merge(
Observable.just("Hello", "RxJava"),
Observable.just("I have", "Started ","RxJava")
).subscribe(s->System.out.println(s));
}
}
|
Output
Hello
RxJava
I have
Started
RxJava
|
Zip: extension method brings together two sequences of values as pairs
public class RxCombine {
public static void main(String args[])
{
List zippedStrings = new ArrayList<>();
Observable.zip(
Observable.just( "Hello", "Hello"),
Observable.just( "I have", "Started ","RxJava"),
(str1, str2) -> str1 + " " + str2).subscribe(s->System.out.println(s));
}
}
|
Output
Hello I have
Hello Started
|
RxJava Tutorial
RxJava - Environment Setup
RxJava’s Characteristics
RxJava - How Observable works
RxJava - Single Observable
RxJava - MayBe Observable
RxJava - Completable Observable
RxJava - Using CompositeDisposable
RxJava - Creating Operators
RxJava - Transforming Operators
RxJava - Filtering Operators
RxJava - Combining Operators
RxJava - Utility Operators
RxJava - Conditional Operators
RxJava - Mathematical Operators
RxJava - Subjects
RxJava - PublishSubject
RxJava - BehaviorSubject
RxJava - AsyncSubject
RxJava - ReplaySubject
RxJava - Schedulers
RxJava - Trampoline Scheduler
RxJava - NewThread Scheduler
RxJava - Computation Scheduler
RxJava - IO Scheduler
RxJava - From Scheduler
RxJava - Buffering