这两个函数似乎非常相似.它们具有相同的签名(接受rx.functions.Func1<? super T, ? extends Observable<? extends R>> func),并且它们的大理石图看起来完全相同.无法在此处粘贴图片,但这是 concatMap ,这里是 flatMap 的一个.生成的Observable的描述似乎有些微妙的区别,其中concatMap生成的一个包含通过合并生成的Observable所产生的项目,而flatMap生成的一个包含通过首先合并生成的Observables所生成的项目. ,并发出该合并的结果.
It seems that these 2 functions are pretty similar. They have same signature (accepting rx.functions.Func1<? super T, ? extends Observable<? extends R>> func), and their marble diagrams look exactly same. Can't paste the pics here, but here's one for concatMap, and here's one for flatMap. There seems to be some subtle difference in the description of resulting Observable, where one produced by concatMap contains items that result from concatinating resulting Observables, and the one produced by flatMap contains items that result from first merging the resulting Observables, and emitting the result of that merger.
但是,这种微妙之处对我来说还不清楚.任何人都可以对这种差异进行更好的解释,理想情况下可以举例说明这种差异.
However, this subtlety is totally unclear to me. Can anyone give a better explanation of this difference, and ideally give some examples illustrating this difference.
推荐答案如您所写,这两个函数非常相似,而细微的区别是创建输出的方式(应用映射功能后).
As you wrote, the two functions are very similar and the subtle difference is how the output is created ( after the mapping function is applied).
平面地图使用合并运算符,而concatMap使用 concat运算符.
Flat map uses merge operator while concatMap uses concat operator.
如您所见,concatMap输出序列是有序的-第一个Observable发出的所有项目都在第二个Observable发出的所有项目之前发出, 合并flatMap输出序列时-合并的Observable发出的项目可能以任何顺序出现,而不管它们来自哪个源Observable.
As you see the concatMap output sequence is ordered - all of the items emitted by the first Observable being emitted before any of the items emitted by the second Observable, while flatMap output sequence is merged - the items emitted by the merged Observable may appear in any order, regardless of which source Observable they came from.
更多推荐
RxJava中的concatMap和flatMap有什么区别
发布评论