在 RxJava 中将 Observable<List<Car>> 转换为 Observable<Car> 的序列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29672705/
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
Convert Observable<List<Car>> to a sequence of Observable<Car> in RxJava
提问by Giorgio
Given a list of cars (List<Car> cars
), I can do:
给定汽车列表 ( List<Car> cars
),我可以执行以下操作:
Observable.just(cars); //returns an Observable that emits one List<Car>
Observable.from(cars); //returns an Observable that emits a squence of Car
Is there a way I can go from an Observable
of a List<Car>
to a sequence of Observable<Car>
?
有没有办法可以从Observable
aList<Car>
到 a 序列Observable<Car>
?
Something like a from
without parameters
像一个from
没有参数的东西
Obserable.just(cars).from()
采纳答案by Egor Neliuba
You can map an Observable<List<Car>>
to Observable<Car>
like so:
可映射Observable<List<Car>>
到Observable<Car>
像这样:
yourListObservable.flatMapIterable(x -> x)
Note that flatMapping might not preserve the order of the source observable. If the order matters to you, use concatMapIterable
. Read herefor more details.
请注意, flatMapping 可能不会保留源 observable 的顺序。如果订单对您很重要,请使用concatMapIterable
。阅读此处了解更多详情。
回答by Mohammad Sultan Al Nahiyan
you can use this
你可以用这个
flatMap { t -> Observable.fromIterable(t) }