
1. Overview
In this article, we will learn the RxJava Just operator. The RxJava operators create a new observable
, transform or change the data from one form to another form.
The RxJava Just
operator creates a new observable
by taking a pre-existing object and emitting that specific object to the downstream consumer upon subscription.
Most operators operate on an Observable
and return an Observable
. This allows you to apply these operators one after the other, in a chain. Each operator in the chain changes the Observable
that result from the operation of the previous operator.
2. RxJava Just operator

The RxJava Just operator creates new Observables
by converting an object or a set of objects into an Observable
and emits that or those objects.
You can pass up to a maximum of 10 objects.
For example, the following code creates an Observable that emits the value of type String
.
Observable<String> observable = Observable.just("1");
Let’s create an observer to consume the items emitted by the above observable.
Observer<String> observer = new Observer<String>() { @Override public void onSubscribe(@NonNull Disposable d) { Log.d(TAG, "onSubscribe"); } @Override public void onNext(@androidx.annotation.NonNull String o) { Log.d(TAG, "onNext : $o"); } @Override public void onError(@NonNull Throwable e) { Log.d(TAG, "onError : $e"); } @Override public void onComplete() { Log.d(TAG, "onComplete"); } }; observable.subscribe(observer);
If you execute the above code, you would notice the following logs:
2022-04-30 15:14:44.306 28769-28769/com.tedblob.rxjava D/MainActivity: onSubscribe 2022-04-30 15:14:44.306 28769-28769/com.tedblob.rxjava D/MainActivity: onNext : 1 2022-04-30 15:14:44.306 28769-28769/com.tedblob.rxjava D/MainActivity: onComplete
Just
will simply emit the array or iterable or whatever you provide as it is, unchanged, as a single item.
For example, the Just
operator in the following code takes the array list as input and emit the entire array list as a single item. If you want to take the array list and emit each element in the array list as a sequence, then use the From
operator.
List<String> stringArrayList = Arrays.asList("1", "2", "3", "4"); Observable<List<String>> observable = Observable.just(stringArrayList);
The observer would be like below.
Observer<List<String>> observer = new Observer<List<String>>() { @Override public void onSubscribe(@NonNull Disposable d) { Log.d(TAG, "onSubscribe"); } @Override public void onNext(@NonNull List<String> strings) { Log.d(TAG, "onNext : " + strings.toString()); } @Override public void onError(@NonNull Throwable e) { Log.d(TAG, "onError : $e"); } @Override public void onComplete() { Log.d(TAG, "onComplete"); } }; observable.subscribe(observer);
Note that if you pass null
to Just, it will return an Observable that emits null
as an item. Do not make the mistake of assuming that this will return an empty Observable (one that emits no items at all). For that, you will need the Empty operator.
However, the arguments of the just
operator are annotated with @NonNull
annotation in RxJava library so you can’t pass null
value.
Observable<String> observable = Observable.just(null); // causes compile time error Caused by: java.lang.NullPointerException: item is null at java.util.Objects.requireNonNull(Objects.java:245) at io.reactivex.rxjava3.core.Observable.just(Observable.java:2624) at com.tedblob.rxjava.MainActivity.onCreate(MainActivity.kt:15)
4. Conclusion
To sum up, we have learned the RxJava Just operator with examples. To learn more about other RxJava topics, refer to these articles.
Pingback: RxJava fromArray operator - TedBlob