February 2020
Intermediate to advanced
412 pages
9h 36m
English
For a given Observable<T>, the toMap() operator collects received values into Map<K,T>, where K is the key type. The key is generated by the Function<T,K> function provided as the argument. For example, 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.rxjava3.core.Observable;public class Ch3_34 { public static void main(String[] args) { Observable.just("Alpha", "Beta", "Gamma") .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, G=Gamma}
The s -> s.charAt(0) lambda argument takes each received String ...
Read now
Unlock full access