Combining 'n' Observables of the same type (RxJava)

时光怂恿深爱的人放手 提交于 2020-01-15 07:46:37

问题


I have a code flow that generates an Iterable of Observables of the same type. I then go through them all, combining them and returning the result as an Observable. At the moment I'm using zip with a FuncN, which seems horrible and I think I've missed the point somewhere. Here's an example that uses a Map, it's obviously nonsense but you get the idea.

final ImmutableList.Builder<Map<String, Object>> observables = 
        ImmutableList.builder();
for (String key: keys) {
    if (someTest(key)) {
        observables.add(generateObservableMap(key));
    }
}

return Observable.zip(observables.build(), data -> {
    final Map<String, Object> result = Maps.newHashMap();

    // THIS IS REALLY UGLY
    for (Object d: data) {
        for (Object e: ((Map) d).entrySet()) {
            final Map.Entry entry = (Map.Entry) e;
            final String key = (Model) entry.getKey();
            final Object value = (AuthData) entry.getValue();
            result.put(key, value);
        }
    }

    return ImmutableMap.copyOf(result);
});

I'm sure there's a better way of doing this.


回答1:


This is actually more a question about Java Collections than about RxJava. The part that you labelled as REALLY UGLY can be made a bit nicer using putAll:

@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> mergeMaps(Object... maps) {
    final Map<K, V> result = new HashMap<K, V>();
    for (Object map: maps) {
        // unchecked <K,V> require @SuppressWarnings
        result.putAll((Map<K,V>)map);
    }
    return result;
}

Notice that if the same key occurs in several maps, only the last value for it will be in the result.

You may wonder why I chose Object... as argument type instead of Map<K,V>.... The reason is that FuncN will have an Object[], which cannot be cast to a Map<K,V>[], because this would throw a ClassCastException, even if all elements in the array are of type Map.

Now if you have some sampleObservables inside an Iterable<Observable<Map<String, Object>>>, you can use zip and mergeMaps as follows:

return Observable.zip(sampleObservables, new FuncN<Map<String, Object>>() {
    public Map<String, Object> call(Object... args) {
        return mergeMaps(args);
    }
});


来源:https://stackoverflow.com/questions/26131429/combining-n-observables-of-the-same-type-rxjava

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!