【问题标题】:RxJava2 zip two Flowables into oneRxJava2 将两个 Flowable 压缩为一个
【发布时间】:2017-10-09 15:21:01
【问题描述】:

我正在努力寻找将两个 Flowable 压缩为一个的任何 RxJava2 示例。

我正在尝试修改 this test 以包含类似于

的内容
    Integer[] ints = new Integer[count];
    Integer[] moreints = new Integer[count];
    Arrays.fill(ints, 777);
    Arrays.fill(moreints, 777);

    Flowable<Integer> source = Flowable.fromArray(ints);
    Flowable<Integer> anothersource = Flowable.fromArray(moreints);

    Flowable<Integer> zippedsources = Flowable.zip(source, anothersource,
            new BiFunction<Flowable<Integer>, Flowable<Integer>, Flowable<Integer>>() {

                @Override
                public void apply(Flowable<Integer> arg0, Flowable<Integer> arg1) throws Exception {
                    return arg0.blockingFirst() + arg1.blockingLast();
                }

    }).runOn(Schedulers.computation()).map(this).sequential();

编辑:我试图从源和另一个源中获取一个整数并将它们相加,但它似乎与 RxJava1 这样做的方式根本不同......我尝试了一堆返回 Integer、Publisher、Flowable 和 void 的变体但在 Eclipse 中,zip 运算符本身不断出现错误。

我无法弄清楚.zip(Iterable&lt;? extends Publisher&lt;? extends T&gt;&gt;, Function&lt;? super Object[], ? extends R&gt;). 中的内容是什么

【问题讨论】:

  • 你期待什么结果,你现在有什么结果?
  • 尝试BiFunction&lt;Integer, Integer, Integer&gt; 并调整apply 方法的类型。压缩函数获取的不是源Flowables,而是每次调用一个值。
  • 谢谢@akarnokd - 这正是我的误解。

标签: java rx-java2 reactivex


【解决方案1】:

由于你只需要压缩两个flowable,你可以使用Flowable.zipWith Operator。

使用方法如下:

source.zipWith(anotherSource, new BiFunction<Integer, Integer, Integer>() {
    @Override public Integer apply(Integer a, Integer b) {
        return a + b;
    } 
};

【讨论】:

  • 感谢BiFunction
猜你喜欢
  • 1970-01-01
  • 2023-04-03
  • 2011-08-26
  • 1970-01-01
  • 2017-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多