java 何时使用 zip() 而不是 zipWith() RxJava
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45575919/
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
When to use zip() instead of zipWith() RxJava
提问by Stuckzilla
Is there any semantic difference in pairwise composing with zip()
vs. zipWith(
) in RxJava? Is the choice between the static zip and the .zipWith purely stylistic?
在 RxJava 中,zip()
与zipWith(
)成对组合是否存在语义差异?静态 zip 和 .zipWith 之间的选择纯粹是风格吗?
回答by akarnokd
Convenience and context.
方便和上下文。
The static zip
is useful when you have two sources already assembled and now you want to zip them together. Most of the time they are themselves long chains or come from all over the place.
zip
当您已经组装了两个源并且现在想要将它们压缩在一起时,静态非常有用。大多数时候,他们本身就是长链或来自各地。
Observable<T1> source1 = op().op().op().op().op();
Observable<T2> source2 = op().op().op().op().op();
Observable.zip(source1, source2, (a, b) -> a + b);
The instance zipWith
is useful when one of the sources is longer while the other is shorter. At that point, it is more convenient to zip with the shorter one.
zipWith
当一个源较长而另一个较短时,该实例很有用。在这一点上,用较短的拉链更方便。
public Observable<R> withIndex(Observable<T> source, Func2<Integer, T, R> func) {
return source.zipWith(Observable.range(0, Integer.MAX_VALUE),
(t, idx) -> func(idx, t));
}