In this article we are going to cover just, from, range and repeat operators. These operators are used to create an Observable from a set of items. Using these operators is pretty simple as they won’t do any complex operations other than creating an Observable.

If you are new to RxJava operators, Operators Introduction is a good place to get started.

1. Just()

Just() operator takes a list of arguments and converts the items into Observable items. It takes arguments between one to ten (But the official document says one to nine 🙁 , may be it’s language specific).

Let’s consider the below example. Here an Observable is created using just() from a series of integers. The limitation of just() is, you can’t pass more than 10 arguments.

Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Integer integer) {
                        Log.d(TAG, "onNext: " + integer);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
Output:
onNext: 1
onNext: 2
.
.
onNext: 9
onNext: 10

The below example creates an Observable from an array. Here you should notice that the array is emitted as single item instead of individual numbers.

  • The Observer emits the array onNext(Integer[] integers) so you will always have 1 emission irrespective of length of the array.
Integer[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

Observable.just(numbers)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Integer[]>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Integer[] integers) {
                        Log.d(TAG, "onNext: " + integers.length);

                        // you might have to loop through the array
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
Output:
onNext: 12

2. From()

Unlike just, From() creates an Observable from set of items using an Iterable, which means each item is emitted one at a time.

  • Notice that we are using fromArray() operator as in RxJava2 we have don’t have from(). There is also fromCallable(), fromFuture(), fromIterable() and fromPublisher() operators available.
Integer[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

Observable.fromArray(numbers)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Integer integer) {
                        Log.d(TAG, "onNext: " + integer);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
Output:
onNext: 1
onNext: 2
.
.
onNext: 11
onNext: 12

Difference between just() and from()

All though just() and from() appears to be doing the same work, it differs in number of emissions.

  • just() – Makes only 1 emission. .just(new Integer[]{1, 2, 3}) makes one emission with Observer callback as onNext(Integer[] integers)
  • fromArray() – Makes N emissions. .fromArray(new Integer[]{1, 2, 3}) makes three emission with Observer callback as onNext(Integer integer)

3. Range()

Range() creates an Observable from a sequence of generated integers. The function generates sequence of integers by taking starting number and length. So the above same examples can be modified as Observable.range(1, 10).

Observable.range(1, 10)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Integer integer) {
                        Log.d(TAG, "onNext: " + integer);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
Output:
onNext: 1
onNext: 2
.
.
onNext: 9
onNext: 10

4. Repeat()

Repeat() creates an Observable that emits an item or series of items repeatedly. You can also pass an argument to limit the number of repetitions.

The below example repeats the emission of integers from 1-4 three times using repeat(3).

       Observable
                .range(1, 4)
                .repeat(3)
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(TAG, "Subscribed");
                    }

                    @Override
                    public void onNext(Integer integer) {
                        Log.d(TAG, "onNext: " + integer);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "Completed");
                    }
                });
Output
Subscribed
onNext: 1
onNext: 2
onNext: 3
onNext: 4
onNext: 1
onNext: 2
onNext: 3
onNext: 4
onNext: 1
onNext: 2
onNext: 3
onNext: 4
Completed

That’s all for today! If you want to explore more, have look at other available operators here.

Author