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
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
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With