June 2017
Intermediate to advanced
400 pages
8h 44m
English
A different flavor of toList() is toSortedList(). This will collect the emissions into a list that sorts the items naturally based on their Comparator implementation. Then, it will emit that sorted List<T> forward to the Observer:
import io.reactivex.Observable;public class Launcher { public static void main(String[] args) { Observable.just(6, 2, 5, 7, 1, 4, 9, 8, 3) .toSortedList() .subscribe(s -> System.out.println("Received: " + s)); }}
The output of the preceding code snippet is as follows:
Received: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Like sorted(), you can provide a Comparator as an argument to apply a different sorting logic. You can also specify an initial capacity for the backing ArrayList just like toList().
Read now
Unlock full access