@@ -20,6 +20,8 @@ import rx.functions.FuncN
20
20
import rx .Observable .OnSubscribeFunc
21
21
import rx .lang .scala .observables .ConnectableObservable
22
22
import scala .concurrent .duration
23
+ import java .util
24
+ import collection .JavaConversions ._
23
25
24
26
25
27
/**
@@ -2353,7 +2355,7 @@ trait Observable[+T]
2353
2355
*
2354
2356
* @param index
2355
2357
* the zero-based index of the item to retrieve
2356
- * @param defaultValue
2358
+ * @param default
2357
2359
* the default item
2358
2360
* @return an Observable that emits the item at the specified position in the sequence emitted by the source
2359
2361
* Observable, or the default item if that index is outside the bounds of the source sequence
@@ -2364,6 +2366,69 @@ trait Observable[+T]
2364
2366
val thisJava = asJavaObservable.asInstanceOf [rx.Observable [U ]]
2365
2367
toScalaObservable[U ](thisJava.elementAtOrDefault(index, default))
2366
2368
}
2369
+
2370
+ /**
2371
+ * Return an Observable that emits a single Map containing all items emitted by the source Observable,
2372
+ * mapped by the keys returned by a specified {@code keySelector} function.
2373
+ * <p>
2374
+ * <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/toMap.png">
2375
+ * <p>
2376
+ * If more than one source item maps to the same key, the Map will contain the latest of those items.
2377
+ *
2378
+ * @param keySelector
2379
+ * the function that extracts the key from a source item to be used in the Map
2380
+ * @return an Observable that emits a single item: a Map containing the mapped items from the source
2381
+ * Observable
2382
+ */
2383
+ def toMap [K ] (keySelector : T => K ): Observable [Map [K , T ]]= {
2384
+ val thisJava = asJavaObservable.asInstanceOf [rx.Observable [T ]]
2385
+ val o : rx.Observable [util.Map [K , T ]] = thisJava.toMap[K ](keySelector)
2386
+ toScalaObservable[util.Map [K ,T ]](o).map(m => m.toMap)
2387
+ }
2388
+
2389
+ /**
2390
+ * Return an Observable that emits a single Map containing values corresponding to items emitted by the
2391
+ * source Observable, mapped by the keys returned by a specified {@code keySelector} function.
2392
+ * <p>
2393
+ * <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/toMap.png">
2394
+ * <p>
2395
+ * If more than one source item maps to the same key, the Map will contain a single entry that
2396
+ * corresponds to the latest of those items.
2397
+ *
2398
+ * @param keySelector
2399
+ * the function that extracts the key from a source item to be used in the Map
2400
+ * @param valueSelector
2401
+ * the function that extracts the value from a source item to be used in the Map
2402
+ * @return an Observable that emits a single item: a HashMap containing the mapped items from the source
2403
+ * Observable
2404
+ */
2405
+ def toMap [K , V ] (keySelector : T => K , valueSelector : T => V ) : Observable [Map [K , V ]] = {
2406
+ val thisJava = asJavaObservable.asInstanceOf [rx.Observable [T ]]
2407
+ val o : rx.Observable [util.Map [K , V ]] = thisJava.toMap[K , V ](keySelector, valueSelector)
2408
+ toScalaObservable[util.Map [K , V ]](o).map(m => m.toMap)
2409
+ }
2410
+
2411
+ /**
2412
+ * Return an Observable that emits a single Map, returned by a specified {@code mapFactory} function, that
2413
+ * contains keys and values extracted from the items emitted by the source Observable.
2414
+ * <p>
2415
+ * <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/toMap.png">
2416
+ *
2417
+ * @param keySelector
2418
+ * the function that extracts the key from a source item to be used in the Map
2419
+ * @param valueSelector
2420
+ * the function that extracts the value from the source items to be used as value in the Map
2421
+ * @param mapFactory
2422
+ * the function that returns a Map instance to be used
2423
+ * @return an Observable that emits a single item: a Map that contains the mapped items emitted by the
2424
+ * source Observable
2425
+ */
2426
+ def toMap [K , V ] (keySelector : T => K , valueSelector : T => V , mapFactory : () => Map [K , V ]): Observable [Map [K , V ]] = {
2427
+ val thisJava = asJavaObservable.asInstanceOf [rx.Observable [T ]]
2428
+ val o : rx.Observable [util.Map [K , V ]] = thisJava.toMap[K , V ](keySelector, valueSelector)
2429
+ toScalaObservable[util.Map [K , V ]](o).map(m => mapFactory() ++ m.toMap)
2430
+ }
2431
+
2367
2432
}
2368
2433
2369
2434
/**
0 commit comments