The call to api.bookings returns a list of bookings in a predefined order. The call to api.order() returns a booking but with some extra info. After applying flatMap() the order of the bookings list gets messed up. (bookings.toList() is just a list if Bookings). How can I get the values in the zip function in the order as emitted by .from(bookings.toList()). Note: I can't use toSortedList as I have no way to compare two bookings.
bookingsSubscription = api.bookings()
.flatMap(bookings ->
Observable.zip(
Observable
.from(bookings.toList())
.flatMap(booking -> api.order(booking.orderId)),
Observable.from(bookings.toList()),
(newBooking, oldBooking) -> {
// at this point newBooking != oldBooking
}
).toList())
.subscribe(callback);
api.order
Does flatmap() method preserve the order of the streams? Yes, It does and map() also.
map() function produces one output for one input value, whereas flatMap() function produces an arbitrary no of values as output (ie zero or more than zero) for each input value. Where R is the element type of the new stream.
flatMap() is an intermediate operation and return a new Stream. It returns a Stream consisting of the results of replacing each element of the given stream with the contents of a mapped stream produced by applying the provided mapping function to each element.
In Java 8 Streams, the flatMap() method applies operation as a mapper function and provides a stream of element values. It means that in each iteration of each element the map() method creates a separate new stream. By using the flattening mechanism, it merges all streams into a single resultant stream.
concatMap
will work but won't give the concurrency which you want. concatMapEager
on the other hand will.
An equivalent option is to create a List<Observable>
manually and use Observable.zip(Iterable, FuncN)
to combine the list. This will have the same effect as concatMapEager
.
See RxJava issue 3017 for a discussion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With