June 2017
Intermediate to advanced
400 pages
8h 44m
English
For a given Observable<T>, the toMap() operator will collect emissions into Map<K,T>, where K is the key type derived off a lambda Function<T,K> argument producing the key for each emission.
If we want to collect strings into Map<Char,String>, where each string is keyed off their first character, we can do it like this:
import io.reactivex.Observable;public class Launcher { public static void main(String[] args) { Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon") .toMap(s -> s.charAt(0)) .subscribe(s -> System.out.println("Received: " + s)); }}
The output of the preceding code snippet is as follows:
Received: {A=Alpha, B=Beta, D=Delta, E=Epsilon, G=Gamma}
The s -> s.charAt(0) lambda argument takes each ...
Read now
Unlock full access