I know how to transform Map<X, Y>
to Map<X, Z>
using stream and it there was also a previous question about this.
我知道如何将Map
But I want to learn how do it using RxJava.
但是我想学习如何使用RxJava。
And to be more specific:
更具体地说:
//Input:
Map<String,String> in; // map from key to a string number, for example: "10"
//transform: in -> out
//Output:
Map<String,Integer> out; //map from the same key to Integer.parseInt(val) (invariant: val is a string number)
2 个解决方案
#1
1
You can use Observable.toMap
.
您可以使用Observable.toMap。
I think this is a minimal example:
我认为这是一个最小的例子:
Map<String, String> test = new HashMap<String, String>() {
{
put("1", "1");
put("2", "2");
}
};
Map<String, Integer> res = Observable.from(test.entrySet()).toMap(e -> e.getKey(), e -> Integer.parseInt(e.getValue())).toBlocking().first();
#2
2
I think Dávid Karnok is right in a sense that it is not particularly useful case for RxJava if your only goal is to convert a single map. It might make more sense if you have Observable of (multiple) maps.
我认为David Karnok的观点是对的,如果你的唯一目标是转换一个地图,那么对于RxJava就不是特别有用的。如果你能观察到(多个)地图,可能会更有意义。
Then I would recommend to use a convenience of Guava transformers: https://google.github.io/guava/releases/snapshot/api/docs/com/google/common/collect/Maps.html#transformValues(java.util.Map,%20com.google.common.base.Function)
然后,我建议使用方便的番石榴变压器:https://google.github.io/guava/releases/snapshot/api/docs/com/google/common/collect/Maps.html#transformValues(java.util.Map,% 20com.common.base.function)
Map<String,String> source1 = ...
Map<String,String> source2 = ...
Observable.just(source1, source2)
.map(map->Maps.transformValues(map, v->Integer.parseInt(v)));
One nice thing about Guava's map value transformer is that it does not recreate map data structure, but instead creates a transformed view which maps values lazily (on the fly).
番石榴的映射值转换器的一个好处是,它不重新创建映射数据结构,而是创建一个转换后的视图,该视图缓慢地(动态地)映射值。
If you do not want to depend on Guava, you can just trivially implement Func1<Map<String,String>,Map<String,Integer>>
yourself.
如果您不想依赖于番石榴,您可以简单地实现Func1
#1
1
You can use Observable.toMap
.
您可以使用Observable.toMap。
I think this is a minimal example:
我认为这是一个最小的例子:
Map<String, String> test = new HashMap<String, String>() {
{
put("1", "1");
put("2", "2");
}
};
Map<String, Integer> res = Observable.from(test.entrySet()).toMap(e -> e.getKey(), e -> Integer.parseInt(e.getValue())).toBlocking().first();
#2
2
I think Dávid Karnok is right in a sense that it is not particularly useful case for RxJava if your only goal is to convert a single map. It might make more sense if you have Observable of (multiple) maps.
我认为David Karnok的观点是对的,如果你的唯一目标是转换一个地图,那么对于RxJava就不是特别有用的。如果你能观察到(多个)地图,可能会更有意义。
Then I would recommend to use a convenience of Guava transformers: https://google.github.io/guava/releases/snapshot/api/docs/com/google/common/collect/Maps.html#transformValues(java.util.Map,%20com.google.common.base.Function)
然后,我建议使用方便的番石榴变压器:https://google.github.io/guava/releases/snapshot/api/docs/com/google/common/collect/Maps.html#transformValues(java.util.Map,% 20com.common.base.function)
Map<String,String> source1 = ...
Map<String,String> source2 = ...
Observable.just(source1, source2)
.map(map->Maps.transformValues(map, v->Integer.parseInt(v)));
One nice thing about Guava's map value transformer is that it does not recreate map data structure, but instead creates a transformed view which maps values lazily (on the fly).
番石榴的映射值转换器的一个好处是,它不重新创建映射数据结构,而是创建一个转换后的视图,该视图缓慢地(动态地)映射值。
If you do not want to depend on Guava, you can just trivially implement Func1<Map<String,String>,Map<String,Integer>>
yourself.
如果您不想依赖于番石榴,您可以简单地实现Func1