-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Operator: ToDictionary #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I believe the equivalent to IDictionary is a Map in Java, but there are a lot of map classes (Linked, Concurrent, etc.). Which one should be the default value type? Is it okay to call the methods Observable.toMap() ? |
I vote for java.util.HashMap by default with an overload that lets you pass in a map class of any type you want.
PS: not sure if I got the co/contra-variance right on that. |
Two questions:
|
This is how I did it somewhere else (below):
public class OperationToMap {
public static class ToMap<T, K, V> implements OnSubscribeFunc<Map<K, V>> {
private final Observable<T> source;
private final Func1<? super T, ? extends K> keySelector;
private final Func1<? super T, ? extends V> valueSelector;
private final Func0<? extends Map<K, V>> mapFactory;
public ToMap(
Observable<T> source,
Func1<? super T, ? extends K> keySelector,
Func1<? super T, ? extends V> valueSelector,
Func0<? extends Map<K, V>> mapFactory
) {
this.source = source;
this.keySelector = keySelector;
this.valueSelector = valueSelector;
this.mapFactory = mapFactory;
}
@Override
public Subscription onSubscribe(Observer<? super Map<K, V>> t1) {
return source.subscribe(new ToMapObserver(t1));
}
public class ToMapObserver implements Observer<T> {
Map<K, V> map;
private final Observer<? super Map<K, V>> t1;
public ToMapObserver(Observer<? super Map<K, V>> t1) {
map = mapFactory.call();
this.t1 = t1;
}
@Override
public void onCompleted() {
Map<K, V> map0 = map;
map = null;
t1.onNext(map0);
t1.onCompleted();
}
@Override
public void onError(Throwable e) {
map = null;
t1.onError(e);
}
@Override
public void onNext(T args) {
K key = keySelector.call(args);
V value = valueSelector.call(args);
map.put(key, value);
}
}
}
} |
http://msdn.microsoft.com/en-us/library/hh229137(v=vs.103).aspx
http://msdn.microsoft.com/en-us/library/hh212046(v=vs.103).aspx
http://msdn.microsoft.com/en-us/library/hh212075(v=vs.103).aspx
http://msdn.microsoft.com/en-us/library/hh229073(v=vs.103).aspx
The text was updated successfully, but these errors were encountered: