Concat VS Merge运算符 [英] Concat VS Merge operator

查看:113
本文介绍了Concat VS Merge运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检查RXJava的文档,我注意到concat和merge运算符似乎也是这样。
我写了几个测试以确定。

  @Test 
public void testContact(){

Observable.concat(Observable.just(Hello),
Observable.just(reactive),
Observable.just(world))
.subscribe(的System.out ::的println);
}

@Test
public void testMerge(){

Observable.merge(Observable.just(Hello),
Observable.just(reactive),
Observable.just(world))
.subscribe(System.out :: println);
}

文档说



< blockquote>

Merge运算符也类似。它结合了两个或多个Observable的发射,但可以交错它们,而Concat从不交错多个Observable的发射。


但仍然我不完全明白,运行这个测试千次,合并结果总是一样的。由于订单未被授予,我有时会期待反应性世界问候。



代码在这里
合并




I was checking the documentation of RXJava and I notice that concat and merge operators seems do the same. I wrote a couple test to be sure.

@Test
public void testContact() {

    Observable.concat(Observable.just("Hello"),
                      Observable.just("reactive"),
                      Observable.just("world"))
              .subscribe(System.out::println);
}

@Test
public void testMerge() {

    Observable.merge(Observable.just("Hello"),
                      Observable.just("reactive"),
                      Observable.just("world"))
            .subscribe(System.out::println);
}

The documentation says

The Merge operator is also similar. It combines the emissions of two or more Observables, but may interleave them, whereas Concat never interleaves the emissions from multiple Observables.

But still I don't full understand, running this test thousand of times the merge result always is the same. Since the order is not granted I was expecting sometimes "reactive" "world" "hello" for example.

The code is here https://github.com/politrons/reactive

解决方案

It is as described in documentation you have quoted - merge can interleave the outputs, while concat will first wait for earlier streams to finish before processing later streams. In your case, with single-element, static streams, it is not making any real difference (but in theory, merge could output words in random order and still be valid according to spec). If you want to see the difference, try following (you will need to add some sleep afterwards to avoid early exit)

    Observable.merge(
            Observable.interval(1, TimeUnit.SECONDS).map(id -> "A" + id),
            Observable.interval(1, TimeUnit.SECONDS).map(id -> "B" + id))
    .subscribe(System.out::println);

A0 B0 A1 B1 B2 A2 B3 A3 B4 A4

versus

    Observable.concat(
            Observable.interval(1, TimeUnit.SECONDS).map(id -> "A" + id),
            Observable.interval(1, TimeUnit.SECONDS).map(id -> "B" + id))
    .subscribe(System.out::println);

A0 A1 A2 A3 A4 A5 A6 A7 A8

Concat will never start printing B, because stream A never finishes.

s/stream/observable/g ;)

Documentation gives nice graphs to show the difference. You need to remember that merge gives no guarantee of interleaving items one by one, it is just an one of possible examples.

Concat

Merge

这篇关于Concat VS Merge运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆