@@ -3913,47 +3913,52 @@ trait Observable[+T]
3913
3913
}
3914
3914
3915
3915
/**
3916
- * Returns an Observable that emits a single `Map`, returned by a specified mapFactory` function, that
3917
- * contains an `Seq` of values, extracted by a specified `valueSelector` function from items
3918
- * emitted by the source Observable and keyed by the `keySelector` function.
3916
+ * Returns an Observable that emits a single `mutable. Map[K, mutable.Buffer[V]] `, returned by a specified ` mapFactory` function, that
3917
+ * contains values, extracted by a specified `valueSelector` function from items emitted by the source Observable and
3918
+ * keyed by the `keySelector` function.
3919
3919
*
3920
3920
* <img width="640" height="305" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/toMultiMap.png">
3921
3921
*
3922
3922
* @param keySelector the function that extracts a key from the source items to be used as the key in the Map
3923
3923
* @param valueSelector the function that extracts a value from the source items to be used as the value in the Map
3924
- * @param mapFactory he function that returns a Map instance to be used
3925
- * @return an Observable that emits a single item: a `Map` that contains a `Seq` items mapped from the source
3926
- * Observable
3924
+ * @param mapFactory he function that returns a `mutable. Map[K, mutable.Buffer[V]]` instance to be used
3925
+ * @return an Observable that emits a single item: a `mutable. Map[K, mutable.Buffer[V]] ` that contains items mapped
3926
+ * from the source Observable
3927
3927
*/
3928
- def toMultimap [K , V ] (keySelector : T => K , valueSelector : T => V , mapFactory : () => mutable. Map [ K , mutable. Buffer [ V ]] ): Observable [scala.collection. Map [ K , Seq [ V ]] ] = {
3929
- toMultimap(keySelector, valueSelector, mapFactory, k => mutable.ListBuffer [V ]())
3928
+ def toMultimap [K , V , M <: mutable. Map [ K , mutable. Buffer [ V ]]] (keySelector : T => K , valueSelector : T => V , mapFactory : () => M ): Observable [M ] = {
3929
+ toMultimap[ K , V , mutable. Buffer [ V ], M ] (keySelector, valueSelector, mapFactory, k => mutable.Buffer [V ]())
3930
3930
}
3931
3931
3932
3932
/**
3933
- * Returns an Observable that emits a single `Map`, returned by a specified `mapFactory` function, that
3934
- * contains a custom `Seq` of values, extracted by a specified `valueSelector` function from
3935
- * items emitted by the source Observable, and keyed by the `keySelector` function.
3933
+ * Returns an Observable that emits a single `mutable. Map[K, B] `, returned by a specified `mapFactory` function, that
3934
+ * contains values extracted by a specified `valueSelector` function from items emitted by the source Observable, and
3935
+ * keyed by the `keySelector` function.
3936
3936
*
3937
3937
* <img width="640" height="305" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/toMultiMap.png">
3938
3938
*
3939
3939
* @param keySelector the function that extracts a key from the source items to be used as the key in the Map
3940
3940
* @param valueSelector the function that extracts a value from the source items to be used as the value in the Map
3941
3941
* @param mapFactory the function that returns a Map instance to be used
3942
- * @param collectionFactory the function that returns a Collection instance for a particular key to be used in the Map
3943
- * @return an Observable that emits a single item: a `Map` that contains the `Seq` of mapped items from
3944
- * the source Observable
3942
+ * @param collectionFactory the function that returns a `mutable.Buffer[V]` instance for a particular key to be used in the Map
3943
+ * @return an Observable that emits a single item: a `mutable.Map[K, B]` that contains mapped items from the source Observable
3945
3944
*/
3946
- def toMultimap [K , V ](keySelector : T => K , valueSelector : T => V , mapFactory : () => mutable.Map [K , mutable.Buffer [V ]], collectionFactory : K => mutable.Buffer [V ]): Observable [scala.collection.Map [K , Seq [V ]]] = {
3945
+ def toMultimap [K , V , B <: mutable.Buffer [V ], M <: mutable.Map [K , B ]](keySelector : T => K , valueSelector : T => V , mapFactory : () => M , collectionFactory : K => B ): Observable [M ] = {
3946
+ // It's complicated to convert `mutable.Map[K, mutable.Buffer[V]]` to `java.util.Map[K, java.util.Collection[V]]`,
3947
+ // so RxScala implements `toMultimap` directly.
3948
+ // Choosing `mutable.Buffer/Map` is because `append/update` is necessary to implement an efficient `toMultimap`.
3947
3949
lift {
3948
- (subscriber : Subscriber [scala.collection. Map [ K , Seq [ V ]] ]) => {
3949
- val map = mapFactory().withDefault(collectionFactory)
3950
+ (subscriber : Subscriber [M ]) => {
3951
+ val map = mapFactory()
3950
3952
Subscriber [T ](
3951
3953
subscriber,
3952
3954
(t : T ) => {
3953
3955
val key = keySelector(t)
3954
- val value = map(key)
3955
- value += valueSelector(t)
3956
- map += key -> value : Unit
3956
+ val values = map.get(key) match {
3957
+ case Some (v) => v
3958
+ case None => collectionFactory(key)
3959
+ }
3960
+ values += valueSelector(t)
3961
+ map += key -> values : Unit
3957
3962
},
3958
3963
e => subscriber.onError(e),
3959
3964
() => {
0 commit comments