RxJava - fetch every item on the list

rxjava2 observable from list
fromiterable rxjava
rxjava loop through list
rxjava collect into list
rxjava flatmap
rxjava map
reactivex list
flowable to list

I have a method that returns an Observable<ArrayList<Long>>, which are ids of some Items. I'd like to go through this list and download every Item using another method that returns Observable<Item>.

How would I do this using RxJava operators?

Here's a small self contained example

public class Example {

    public static class Item {
        int id;

    public static void main(String[] args) {
                .flatMapIterable(ids -> ids) // Converts your list of ids into an Observable which emits every item in the list
                .flatMap(Example::getItemObservable) // Calls the method which returns a new Observable<Item>
                .subscribe(item -> System.out.println("item: " + item.id));

    // Simple representation of getting your ids.
    // Replace the content of this method with yours
    private static Observable<List<Integer>> getIds() {
        return Observable.just(Arrays.<Integer>asList(1, 2, 3));

    // Replace the content of this method with yours
    private static Observable<Item> getItemObservable(Integer id) {
        Item item = new Item();
        item.id = id;
        return Observable.just(item);

Please note that Observable.just(Arrays.<Integer>asList(1, 2, 3)) is a simple representation of Observable<ArrayList<Long>> from your question. You can replace it with your own Observable in your code.

This should give you the basis of what you need.

p/s : Use flatMapIterable method for this case since it belongs to Iterable as below explaining:

 * Implementing this interface allows an object to be the target of
 * the "for-each loop" statement. See
 * <strong>
 * <a href="{@docRoot}openjdk-redirect.html?v=8&path=/technotes/guides /language/foreach.html">For-each Loop</a>
 * </strong>
 * @param <T> the type of elements returned by the iterator
 * @since 1.5
 * @jls 14.14.2 The enhanced for statement
 public interface Iterable<T>

RxJava: flattening a stream of Iterables - Sven Bendel, Today I finally stumbled upon the solution of a really trivial RX problem: Suppose you have an Observable which returns Lists of items. Like Observable >. Observable.fromIterable(list) should emit an event for each item in the list which means that you're return type in getPhotos be Flowable<Bitmap> as Flowable can return more than one value.

Observable<List<X>> to Observable<X> that emits each element in , Is there a utility operator for converting from Observable<List<X>> to an backpressure and emits all elements from the list regardless of how many .com​/wiki/ReactiveX/RxJava/images/rx-operators/mergeMapIterable.png"  Using rxjava to get bitmap for every list item. Ask Question Asked 3 years, 7 months ago. it's always return a list, but i need every item separately.

Use a Transformer that modifies the source Observable, calling a flatMap on it with a function. You can think of this as a 2-step process:

  1. The function takes each emitted item (an Iterable<T>) and re-emits it as an Observable<T>
  2. flatMap takes each of these emitted Observable<T> objects an merges them into a single Observable<T>

The Transformer looks like this:

public class FlattenTransform<T> implements Observable.Transformer<Iterable<T>, T> {
    public Observable<? extends T> call(Observable<? extends Iterable<T>> source) {
        return source.flatMap(new Func1<Iterable<T>, Observable<T>>() {
            public Observable<T> call(Iterable<T> values) {
                return Observable.from(values);

Once you've created your Transformer, you can use compose to apply the transformation on the source observable:

public class Example {

    private static final ArrayList<Long> sourceList = new ArrayList<>(Arrays.asList(new Long[] {1L,2L,3L}));
    private static final Observable<ArrayList<Long>> listObservable = Observable.just(sourceList);
    private static final FlattenTransform<Long> flattenList = new FlattenTransform<Long>();

    public static void main(String[] args) {

    private static Action1<Long> printItem = new Action1<Long>() {
        public void call(Long item) {
            System.out.println("item: " + item);

The advantage of using a compose with a Transformer instead of a flatMap with a Func1 is that if in the future if you need to flatten a list again, you won't even have to think about which operator to use (map? flatMap? concatMap?). In other words, the flatmap operation is baked into the FlattenTransform class and that detail is abstracted away.

Transformers also have other benefits, such as being able to chain together multiple operations.

RxJava, I have a method that returns an Observable<List<Long>> , which are ids of some Items. I'd like to go through this list and download every Item using another  RxJava-fetch every item on the list I have a method that returns an Observable<ArrayList<Long>>, which are ids of some Items. I'd like to go through this list and download every Item using another method that returns Observable<Item>.

In Kotlin use flattenAsFlowable:

    .flattenAsFlowable { it }
    .map { someMethod(it) }
    .subscribe({ },
        { onError(it) })

RxJava, RxJava - fetch every item on the list and emits one-by-one (any order) - android. toList( ) — collect all items from an Observable and emit them as a single List toMap( ) — convert the sequence of items emitted by an Observable into a map keyed by a specified key function toMultimap( ) — convert the sequence of items emitted by an Observable into an ArrayList that is also a map keyed by a specified key function

Understanding RxJava Zip Operator With Example, In this blog, we are going to learn the RxJava Zip Operator with an example. specified function and emit single items for each combination based on the private fun fetchUserListFromNetwork(): List<User> { return listOf() } }. To use RxJava you create Observables (which emit data items), transform those Observables in various ways to get the precise data items that interest you (by using Observable operators), and then observe and react to these sequences of interesting items (by implementing Observers or Subscribers and then subscribing them to the resulting transformed

Reactive Programming with RxJava, Selection from Reactive Programming with RxJava [Book] The resulting stream completes immediately after emitting a single List item. The assumption is that fetching a bestseller is faster and can be cached. concatMap() requires a transformation from List<Person> to Observable<Person> , executed for each page. defaultIfEmpty( ) — emit items from the source Observable, or emit a default item if the source Observable completes after emitting no items ( rxjava-computation-expressions) doWhile( ) — emit the source Observable's sequence, and then repeat the sequence as long as a condition remains true

Using Observables to Render Responsive Lists: An RxJava Case , This is the 1st part of a 3 part series about how RxJava is used in Pre, how we used Observables to compose a complex view that displays a list of items, recent message in the conversation thread for each of your friends. Then for every item that the source observable emits, it will apply a function to that item, and then emit the result on the destination Observable. Operators can be chained together to create complex data flows that filter event based on certain criteria. Multiple operators can be applied to the same observable.

  • Is ordering important?
  • Not really, but for educational purposes is really like to see both versions
  • You could also do Observable.from(Arrays.<Integer>asList(...)) and not need the flatMapIterable
  • You saved my life. Thanks.
  • Great example. Thanks.
  • @Miguel Can you please take a look at this question? stackoverflow.com/questions/48959354/…
  • Please take a look at this question: stackoverflow.com/questions/48959354/…