java RxJava - Just vs From

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30819349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:42:40  来源:igfitidea点击:

RxJava - Just vs From

javarx-java

提问by j2emanue

I'm getting the same output when using Observable.justvs Observable.fromin the following case:

在以下情况下使用Observable.justvs时,我得到相同的输出Observable.from

 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 justsuppose to emit a single item and fromwas to emit items in some sort of list. Whats the difference ? I also noted that justand fromtakes 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 cant define unlimited arguments.

我以为只是just假设要发出一个项目,并且from要在某种列表中发出项目。有什么不同 ?我也注意到了这一点,just并且from只接受了有限数量的论点。所以没问题Observable.just(1,2,3,4,5,6,7,8,-1,-2)Observable.just(1,2,3,4,5,6,7,8,-1,-2,-3)失败了。from也是一样,我必须将它包装在一个列表或数组中。我只是好奇为什么他们不能定义无限的参数。

UPDATE: I experimented and saw that justdoes not take a array structure it justtakes arguments. fromtakes a collection. so the following works for frombut not for just:

更新:我进行了试验,发现它justjust采用带参数的数组结构。from需要一个集合。所以以下适用于from但不适用于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 ?

我假设这是明显的区别,对吗?

回答by Adam S

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

当您查看每个传递 an Iterable(例如 a List)时的行为时,差异应该更清楚:

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

Observable.just(someList)会给你 1 排放 - a List

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

Observable.from(someList)会给你 N 排放 - 列表中的每个项目。

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

将多个值传递给的能力just是一个方便的特性;以下功能相同:

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

回答by Zubair Rehman

Difference between just()and from():

just()和之间的区别from()

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

虽然所有just()from()似乎是做同样的工作,不同之处排放的数量。

just()– Makes only 1 emission. Observable.just(new Integer[]{1, 2, 3})makes one emission with Observer callback as onNext(Integer[] integers)

just()– 仅发射 1 次。Observable.just(new Integer[]{1, 2, 3})使用观察者回调进行一次发射onNext(Integer[] integers)

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

fromArray()– 产生 N 排放。Observable.fromArray(new Integer[]{1, 2, 3})使用观察者回调进行三个发射onNext(Integer integer)

回答by Faisal Naseer

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

inRxJava Just()运算符接受一个参数列表并将这些项目转换为 Observable 项目。它需要一到十个参数(但官方文件说一到九个,可能是语言特定的)。

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

与仅仅不同的是,From()使用 Iterable 从一组项目创建一个 Observable,这意味着每个项目一次发出一个。

回答by ParikshitSinghTomar

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

我们最多只能在just() 中传递 10 个参数,而fromArray具有列表类型。

While internally just()calling fromArray().

而在内部just()调用fromArray()

Check below RxJava Code for just 4 arguments.

检查下面的 RxJava 代码以获取 4 个参数。

 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.

两者都返回相同的可观察对象。

回答by vortex.alex

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

from主要用于数据结构(数组和可迭代)和期货,因此获得的Observable将从这些数据结构或期货中发出单个项目。

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

just无论是数组项还是整数项,都将所有内容视为项。just有一些just变体最多可以接受 10 个参数,因此会产生混淆。

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

因此,实际上,您可能会解释所有这些just变体,例如它们分别发出“仅”一项,或“仅”两项,“仅”三项等等......