Java Concat VS 合并运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38903094/
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
Concat VS Merge operator
提问by paul
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.
我正在检查 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);
}
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.
Merge 运算符也类似。它结合了两个或多个 Observable 的发射,但可能会交错它们,而 Concat 从不交错来自多个 Observable 的发射。
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
采纳答案by Artur Biesiadowski
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)
正如您引用的文档中所述 - 合并可以交错输出,而 concat 将首先等待较早的流完成,然后再处理较晚的流。在您的情况下,对于单元素的静态流,它并没有产生任何真正的区别(但理论上,合并可以按随机顺序输出单词,并且根据规范仍然有效)。如果您想查看差异,请尝试以下操作(之后您需要添加一些睡眠以避免提前退出)
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
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
A0 A1 A2 A3 A4 A5 A6 A7 A8
Concat will never start printing B, because stream A never finishes.
Concat 永远不会开始打印 B,因为流 A 永远不会结束。
s/stream/observable/g ;)
s/stream/observable/g ;)
Documentation gives nice graphs to show the difference. You need to remember that merge gives no guaranteeof interleaving items one by one, it is just an one of possible examples.
文档提供了很好的图表来显示差异。您需要记住,merge 不能保证一个一个地交错项目,它只是一个可能的例子。
Concat
康卡特
回答by Amit Shekhar
Concat
康卡特
Concat emits the emissions from two or more Observables without interleaving them. It will maintain the order of the observables while emitting the items. It means that it will emit all the items of the first observable and then it will emit all the items of the second observable and so on.
Concat 发出来自两个或多个 Observable 的发射,而不将它们交错。它将在发出项目时保持可观察对象的顺序。这意味着它将发出第一个 observable 的所有项目,然后它会发出第二个 observable 的所有项目,依此类推。
Let's understand it clearly by an example.
让我们通过一个例子来清楚地理解它。
final String[] listFirst = {"A1", "A2", "A3", "A4"};
final String[] listSecond = {"B1", "B2", "B3"};
final Observable<String> observableFirst = Observable.fromArray(listFirst);
final Observable<String> observableSecond = Observable.fromArray(listSecond);
Observable.concat(observableFirst, observableSecond)
.subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String value) {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
As we are using Concat Operator, it will maintain the order and emit the values as A1, A2, A3, A4, B1, B2, B3.
当我们使用 Concat Operator 时,它将保持顺序并将值发送为 A1、A2、A3、A4、B1、B2、B3。
Merge
合并
Merge combines multiple Observables into one by merging their emissions. It will not maintain the order while emitting the items.
Merge 通过合并它们的排放将多个 Observable 合并为一个。它不会在发出项目时保持顺序。
Let's understand it clearly by an example.
让我们通过一个例子来清楚地理解它。
final String[] listFirst = {"A1", "A2", "A3", "A4"};
final String[] listSecond = {"B1", "B2", "B3"};
final Observable<String> observableFirst = Observable.fromArray(listFirst);
final Observable<String> observableSecond = Observable.fromArray(listSecond);
Observable.merge(observableFirst, observableSecond)
.subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String value) {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
As we are using Merge Operator, it will not maintain the order and can emit the values in any order such as A1, B1, A2, A3, B2, B3, A4or A1, A2, B1, B2, A3, A4, B3or can be anything.
由于我们使用的是 Merge Operator,它不会保持顺序并且可以按任何顺序发出值,例如A1、B1、A2、A3、B2、B3、A4或A1、A2、B1、B2、A3、A4、B3或者可以是任何东西。
This is how we should use the Concat and the Merge operators in RxJava depending on our use-case.
这就是我们应该根据我们的用例在 RxJava 中使用 Concat 和 Merge 运算符的方式。