How to collect all ints from List<Single<List<Int>>> and put it into List<Int> in RxJava?

Viewed 3271

My goal is to unwrap a list of singles of list of ints, and get all it's elements, in order to put it on a list.

List<Single<List<Int>>> listOfSinglesOfListofInts = getListSingleListType(); // random method that gives us that.
List<Int> intList = new ArrayList<>();

My goal is to move move all Int contents from listOfSinglesOfListOfInts to listInt. Here is what I tried:

ListOfSinglesOfListOfInt.stream().map(
    singleListOfInts -> singleListOfInts.map(
        listOfInts -> intList.addAll(listOfInts)
    )
);


return listInt;

The size of listInt is always 0.

What would be the correct way of accomplishing this?

2 Answers

map operations do not run until the Flowable chain is completed. This Flowable is set up, but is not executed. What you probably want to do is run the Flowable through a blocking collector after flattening the shape. Try this:

return Flowable.fromIterable(listOfSingleOfListofInt)
    .flatMap(singleOfListofInt -> singleOfListofInt.toFlowable())
    .flatMap(listofInt -> Flowable.fromIterable(listofInt))
    .toList()
    .blockingGet();

Details

Flowable.fromIterable(listOfSingleOfListofInt):

  • Transform List<Single<List<Int>>> into Flowable<Single<List<Int>>>

flatMap(singleOfListofInt -> singleOfListofInt.toFlowable()):

  • Transform Flowable<Single<List<Int>>> into Flowable<List<Int>>

flatMap(listofInt -> Flowable.fromIterable(listofInt)):

  • Transform Flowable<List<Int>> into Flowable<Int>

toList():

  • Transform Flowable<Int> into Signle<List<Int>>

blockingGet()

  • Transform Signle<List<Int>> into List<Int>

I did it in Rx-Java.

Lets consider below example to create List<Single<List<Integer>>> 

List<Integer> intList = new ArrayList<>();
Collections.addAll(intList, 10, 20, 30, 40, 50);

List<Integer> intList2 = new ArrayList<>();
Collections.addAll(intList2, 12, 22, 32, 42, 52);

List<Integer> intList3 = new ArrayList<>();
Collections.addAll(intList3, 13, 23, 33, 43, 53);

Single<List<Integer>> singleList = Single.just(intList);
Single<List<Integer>> singleList2 = Single.just(intList2);
Single<List<Integer>> singleList3 = Single.just(intList3);

List<Single<List<Integer>>> listOfSinglesOfListofInts = new ArrayList<>();

Collections.addAll(listOfSinglesOfListofInts, singleList, singleList2, singleList3);


Observable
    //Iterate through List<Singles>
    .fromIterable(listOfSinglesOfListofInts)
    //Get each single, convert to Observable and iterate to get all the integers
    .flatMap(
        singleListNew -> singleListNew
            .toObservable()
            //Iterate through each integer inside a list and return it
            .flatMapIterable(integers -> integers)
    )
    //Get all the integers from above and convert to one list
    .toList()
    //Result is List<Integer>
    .subscribe(
        result -> System.out.println("**** " + result.toString() + " ****"),
        error -> new Throwable(error)
    );

Hope this helps.

Related