Skip to content

RxJava fromIterable

  • by
RxJava fromArray operator

1. Overview

In this article, we will focus on the RxJava fromIterable operator along with a few examples.

It is available in Flowable and Observable.

2. RxJava from operator

The RxJava from operator constructs a sequence from a pre-existing source or generator type. The from is like Just, but note that From will dive into an array or an iterable or something of that sort to pull out items to emit, while Just will simply emit the array or iterable or what-have-you as it is, unchanged, as a single item.

See Just operator.

The fromIterable is a static method and variant of this from operator that accepts only iterable sources as input.

It uses the postfix naming convention (i.e., the method name contains the argument type). Here, Iterable is the argument type and the method name fromIterable contains Iterable to avoid overload resolution ambiguities.

3. RxJava fromIterable operator

The fromIterable creates an Observable / Flowable that emits the items from a java.lang.Iterable source (such as Lists, Sets or Collections or custom Iterables) and then completes the sequence. It do not accept arrays as input.

public static <T> Observable<T> fromIterable(@NonNull Iterable<@NonNull ? extends T> source) 

It emits all the iterable elements as a sequence of elements and completes.

3.1. RxJava fromIterable example

 @Test
    public void testFromListIterable() {
        List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8));

        Observable<Integer> observable = Observable.fromIterable(list);

        observable.subscribe(item -> System.out.println("Item:" + item), error -> error.printStackTrace(),
                () -> System.out.println("Done"));
    }

If you execute the above test case, it prints the following result in the console. As you can see, the Observable created by the fromIterable emits all the list elements as a sequence.

Item:1
Item:2
Item:3
Item:4
Item:5
Item:6
Item:7
Item:8
Done

Observable sources don’t support backpressure. The RxJava 2 represents the backpressure aware sources by using the Flowable.

Backpressure is when your observable (publisher) is creating more events than your subscriber can handle. So your subscribers could miss events or have a huge queue of events which just leads to out of memory, eventually. 

Similar to Observable, you can create a Flowable using the fromIterable operator.

    @Test
    public void testFlowableFromIterable() {
        List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8));

        Flowable<Integer> flowable = Flowable.fromIterable(list);

        flowable.subscribe(item -> System.out.println("Item:" + item), error -> error.printStackTrace(),
                () -> System.out.println("Done"));
    }

The fromIterable comes handy when you want to transform the list item emitted in the previous chain to a sequence of elements. For example, the following code transforms the integerList single item emitted by the Just operator to a sequence of integer elements.

List<Integer> integerList = new ArrayList<>();
Flowable<Integer> intergerListFlowable = 
            Flowable
                    .just(integerList)//emits the list
                    .flatMap(list -> Flowable.fromIterable(list))//emits one by one
                    .map(integer -> integer + 1);

4. Conclusion

To sum up, we have learned the RxJava fromIterable operator with examples. You can find code samples in our GitHub repository.

Leave a Reply

Your email address will not be published. Required fields are marked *