Usando fromIterable, flatMapIterable y flattenAsObservable.

Usando fromIterable, flatMapIterable y flattenAsObservable


fromIterable: Toma una lista de objetos y luego los emite de a uno por uno.
     

       ArrayList list = new ArrayList<>(Arrays.asList("h", "e", "l", "l", "o"));

       Observable.just(list)
                .flatMap(strings -> Observable.fromIterable(strings))
                .map(s -> s.toUpperCase())
                .toList()
                .subscribe(strings -> System.out.println(strings));

Output:
[H, E, L, L, O]   
    


flatMapIterable: Toma una lista de objetos y luego los emite de a uno por uno, sin necesidad de crear un Observable.
     
       ArrayList list = new ArrayList<>(Arrays.asList("h", "e", "l", "l", "o"));

       Observable.just(list)
                .flatMapIterable(strings -> strings)
                .map(s -> s.toUpperCase())
                .toList()
                .subscribe(strings -> System.out.println(strings));
Output:
[H, E, L, L, O]
    

flattenAsObservable: Cumple la misma función que flatMapIterable pero usando un Single.
     
       
       ArrayList list = new ArrayList<>(Arrays.asList("h", "e", "l", "l", "o"));

       Single.just(list)
                .flattenAsObservable(strings -> strings)
                .map(s -> s.toUpperCase())
                .toList()
                .subscribe(strings -> System.out.println(strings));
Output:
[H, E, L, L, O]


Se recomienda utilizar flatMapIterable cada vez que sea posible ya que es un componente optimizado para esta función. Para mayor referencia se puede consultar el PR de David Karnok en optimize concatMapIterable/flatMapIterable.


Métricas tomadas con fromIterable:




Métricas tomadas con flatMapIterable:


Previous
Next Post »
Thanks for your comment