diff --git a/libraries/stdlib/src/kotlin/JUtilCollections.kt b/libraries/stdlib/src/kotlin/JUtilCollections.kt index 6d8114b8d22..72a49f3dca6 100644 --- a/libraries/stdlib/src/kotlin/JUtilCollections.kt +++ b/libraries/stdlib/src/kotlin/JUtilCollections.kt @@ -10,7 +10,7 @@ import java.util.* // /** - * Returns a new List containing the results of applying the given function to each element in this collection + * Returns a new List containing the results of applying the given *transform* function to each element in this collection * * @includeFunctionBody ../../test/CollectionTest.kt map */ @@ -18,7 +18,10 @@ public inline fun java.util.Collection.map(transform : (T) -> R) : jav return mapTo(java.util.ArrayList(this.size), transform) } -/** Transforms each element of this collection with the given function then adds the results to the given collection */ +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ public inline fun > java.util.Collection.mapTo(result: C, transform : (T) -> R) : C { for (item in this) result.add(transform(item)) diff --git a/libraries/stdlib/src/kotlin/JUtilMaps.kt b/libraries/stdlib/src/kotlin/JUtilMaps.kt index b429f71ec8a..a93512e8fe9 100644 --- a/libraries/stdlib/src/kotlin/JUtilMaps.kt +++ b/libraries/stdlib/src/kotlin/JUtilMaps.kt @@ -1,10 +1,11 @@ package kotlin import java.util.Map as JMap -import java.util.HashMap -import java.util.Collections - import java.util.Map.Entry as JEntry +import java.util.Collection +import java.util.Collections +import java.util.HashMap +import java.util.List // Map APIs @@ -71,4 +72,44 @@ public inline fun java.util.Map.getOrPut(key: K, defaultValue: ()-> V public inline fun java.util.Map.iterator(): java.util.Iterator> { val entrySet = this.entrySet()!! return entrySet.iterator()!! -} \ No newline at end of file +} + +/** + * Returns a new List containing the results of applying the given *transform* function to each [[Map.Entry]] in this [[Map]] + * + * @includeFunctionBody ../../test/CollectionTest.kt map + */ +public inline fun java.util.Map.map(transform: (java.util.Map.Entry) -> R) : java.util.List { + return mapTo(java.util.ArrayList(this.size), transform) +} + +/** + * Transforms each [[Map.Entry]] in this [[Map]] with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > java.util.Map.mapTo(result: C, transform: (java.util.Map.Entry) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + + +/** + * Returns a new Map containing the results of applying the given *transform* function to each [[Map.Entry]] in this [[Map]] + * + * @includeFunctionBody ../../test/MapTest.kt map + */ +public inline fun java.util.Map.mapValues(transform : (java.util.Map.Entry) -> R): java.util.Map { + return mapValuesTo(java.util.HashMap(this.size), transform) +} + +/** + * Populates the given *result* [[Map]] with the value returned by applying the *transform* function on each [[Map.Entry]] in this [[Map]] + */ +public inline fun > java.util.Map.mapValuesTo(result: C, transform : (java.util.Map.Entry) -> R) : C { + for (e in this) { + val newValue = transform(e) + result.put(e.key, newValue) + } + return result +} diff --git a/libraries/stdlib/test/MapTest.kt b/libraries/stdlib/test/MapTest.kt index 7368510c507..faea231bfdd 100644 --- a/libraries/stdlib/test/MapTest.kt +++ b/libraries/stdlib/test/MapTest.kt @@ -79,4 +79,36 @@ class MapTest { assertEquals(6, list.size()) assertEquals("beverage,beer,location,Mells,name,James", list.makeString(",")) } + + /* + TODO compiler bug + we should be able to remove the explicit type on the map function + http://youtrack.jetbrains.net/issue/KT-1145 + */ + test fun map() { + val m1 = TreeMap() + m1["beverage"] = "beer" + m1["location"] = "Mells" + + val list = m1.map{ it.value + " rocks" } + + println("Got new list $list") + assertEquals(arrayList("beer rocks", "Mells rocks"), list) + } + + /* + TODO compiler bug + we should be able to remove the explicit type on the mapValues function + http://youtrack.jetbrains.net/issue/KT-1145 + */ + test fun mapValues() { + val m1 = TreeMap() + m1["beverage"] = "beer" + m1["location"] = "Mells" + + val m2 = m1.mapValues{ it.value + "2" } + + println("Got new map $m2") + assertEquals(arrayList("beer2", "Mells2"), m2.values().toList()) + } }