RxJava - Just vs From

20.6k Views Asked by At

I'm getting the same output when using Observable.just vs Observable.from in the following case:

 public void myfunc() {
 //swap out just for from here and i get the same results,why ?
        Observable.just(1,2,3).subscribe(new Subscriber<Integer>() {
            @Override
            public void onCompleted() {
                Log.d("","all done. oncompleted called");
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Integer integer) {
                Log.d("","here is my integer:"+integer.intValue());
            }
        });

    }

I thought just was just supposed to emit a single item and from was to emit items in some sort of list. Whats the difference ? I also noted that just and from take only a limited amount of arguments. So Observable.just(1,2,3,4,5,6,7,8,-1,-2) is ok but Observable.just(1,2,3,4,5,6,7,8,-1,-2,-3) fails. Same goes for from, I have to wrap it in a list or array of sorts. I'm just curious why they can't define unlimited arguments.

UPDATE: I experimented and saw that just does not take a array structure it just takes arguments. from takes a collection. so the following works for from but not for just:

 public Observable myfunc() {
    Integer[] myints = {1,2,3,4,5,6,7,8,-1,-2,9,10,11,12,13,14,15};
   return  Observable.just(myints).flatMap(new Func1<Integer, Observable<Boolean>>() {
        @Override
        public Observable<Boolean> call(final Integer integer) {
            return Observable.create(new Observable.OnSubscribe<Boolean>() {
                @Override
                public void call(Subscriber<? super Boolean> subscriber) {
                    if(integer.intValue()>2){
                        subscriber.onNext(integer.intValue()>2);

                    }
                }
            });
        }
    });

}

I am assuming this to be the clear difference then, correct ?

5

There are 5 best solutions below

2
On BEST ANSWER

The difference should be clearer when you look at the behaviour of each when you pass it an Iterable (for example a List):

Observable.just(someList) will give you 1 emission - a List.

Observable.from(someList) will give you N emissions - each item in the list.

The ability to pass multiple values to just is a convenience feature; the following are functionally the same:

Observable.just(1, 2, 3);
Observable.from(1, 2, 3);
0
On

inRxJava 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).

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

0
On

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. Observable.just(new Integer[]{1, 2, 3}) makes one emission with Observer callback as onNext(Integer[] integers)

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

0
On

from works mostly with data structures (arrays and iterable) and futures, so the obtained Observable will emit single items from those data structures or futures.

just treats everything as item regardless it is an array item or integer item. The confusion around just is generated by the fact that there are a few just variants that can accept up to 10 arguments.

So in fact, you might interpret all those just variants like they emit, respectively, "just" one item, or "just" two items, "just" three items and so on...

0
On

We can just pass max 10 arguments in just() while fromArray have list type.

While internally just() calling fromArray().

Check below RxJava Code for just 4 arguments.

 public static <T> Observable<T> just(T item1, T item2, T item3, T item4) {
        ObjectHelper.requireNonNull(item1, "The first item is null");
        ObjectHelper.requireNonNull(item2, "The second item is null");
        ObjectHelper.requireNonNull(item3, "The third item is null");
        ObjectHelper.requireNonNull(item4, "The fourth item is null");

        return fromArray(item1, item2, item3, item4);
    }

Both are return same observable object.