Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava HashMap Transformation

Supposing i have the following data structure:

Map<String, ObjectA> map;

and the ObjectA:

class ObjectA {
   String a;
   String b;
   int c;    
}

My final goal is to return just:

Map<String, String>

In which the first String is the same as the original, and the second String is the 'String a'. What transformations do i need to accomplish this?

If this was a list, i would have used Observable.from(), take what i want from the single items and then at the end join them together with toList(). But this is a map and i have never nor do i know how to perform iteration over maps in RxJava. Any help would be appreciated

Edit. I know i can do this using Observable.create and use the normal Java/way of doing it like the first answer states. What i really want to know is if there are any Rx Operators that allow me to do that, like Observable.from, flatMap and toList, if i was transforming lists

like image 842
johnny_crq Avatar asked Sep 23 '15 09:09

johnny_crq


1 Answers

Try this code:

Observable.just(map)
        .map(stringObjectAMap -> stringObjectAMap.entrySet())
        .flatMapIterable(entries -> entries)
        .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), e.getValue().a))
        .toMap(e -> e)
        .subscribe(m -> {
            //do something with map
        });
like image 50
Aleksandr Avatar answered Oct 29 '22 22:10

Aleksandr