In this post we will look at functions, which creates Observables from imperative style objects or create Observable with generating items itself.
Basic creation methods
The methods in this section are the basic methods for creating Observable.
From
Simple methods how to create Observable from imperative programming style objects. For example, creating Observable from array or Callable.
Method variantions: fromArray, fromFuture, fromCallable, fromRunnable, …
Examples
Creation of Observable from array:
final Observable<Integer> observable = Observable.fromArray(1, 2, 3);
observable.subscribe(new PrintObserver());
On subscribe.
On next: 1
On next: 2
On next: 3
On completed.
Creation of Observable from callable:
final Observable<Integer> observable = Observable.fromCallable(() -> 6);
observable.subscribe(new PrintObserver());
On subscribe.
On next: 2
On next: 9
On next: 4
On completed.
Creation of Observable from iterable:
final Collection<Integer> iterable = new ArrayList<>();
iterable.add(2); iterable.add(9); iterable.add(4);
final Observable<Integer> observable = Observable.fromIterable(iterable);
observable.subscribe(new PrintObserver());
On subscribe.
On next: 6
On completed.
Just
Create observable from item or items and then the items will be emitted.
Method variantions: just(arrg), just(arg, arg2), just(arg,arg2,arg3), …
Examples
Just method for one argument:
final Observable<Integer> observable = Observable.just(5);
observable.subscribe(new PrintObserver());
On subscribe.
On next: 5
On completed.
Just method with three arguments:
final Observable<Integer> observable = Observable.just(10, 3, 5);
observable.subscribe(new PrintObserver());
On subscribe.
On next: 10
On next: 3
On next: 5
On completed.
Special creation methods
The methods in this section create Observable with generated value defined by the implementation.
Range
Emits sequence of numbers defined by interval as input.
final Observable<Integer> observable = Observable.range(2, 5);
observable.subscribe(new PrintObserver())
On subscribe.
On next: 2
On next: 3
On next: 4
On next: 5
On next: 6
On completed.
Empty
Create Observable, which will emit zero items and completes normally.
final Observable<Integer> observable = Observable.empty();
observable.subscribe(new PrintObserver());
On subscribe.
On completed.
Never
Creates never ending Observable which will never ever emit any item.
final Observable<Integer> observable = Observable.never();
observable.subscribe(new PrintObserver());
On subscribe.
Error
Creates Observable from exception which will be thrown after subscription.
final Observable<Integer> observable = Observable.error(new IllegalArgumentException());
observable.subscribe(new PrintObserver());
On subscribe.
On error: IllegalArgumentException: null
Repeatable based methods
The methods in this section create Observable with generated values from implementation of the method.
Interval
Create Observable which will emit a sequence of numbers spaced with given interval of time.
Method variantions: interval, intervalRange
Observable<Long> observable = Observable.interval(1, 1, TimeUnit.SECONDS);
observable.subscribe(new PrintObserver());
Thread.sleep(3000);
On subscribe.
On next: 0
On next: 1
On next: 2
Observable<Long> observable = Observable.interval(0, 1, TimeUnit.SECONDS);
observable.subscribe(new PrintObserver());
Thread.sleep(3000);
On subscribe.
On next: 0
On next: 1
On next: 2
On next: 3
Repeat
Repeatedly emits specified item.
Timer
Emits one item (specified at calling method) after the specified span of the time.
final Observable<Long> observable = Observable.timer(1, TimeUnit.SECONDS);
observable.subscribe(new PrintObserver());
Thread.sleep(3000);
On subscribe.
//after 2 seconds
On next: 0
On completed.
Other creation methods
Create
Create Observable from scratch by function or ObservableEmitter.
final Observable<Integer> observable = Observable.create(observableEmitter -> {
for (int i = 0; i < 3; i++) {
observableEmitter.onNext(i);
}
});
observable.subscribe(new PrintObserver());
On subscribe.
On next: 0
On next: 1
On next: 2