From 378c95baa3b4eedde968c86e6420bc61372b7d2a Mon Sep 17 00:00:00 2001 From: James Strachan Date: Mon, 16 Apr 2012 17:52:02 +0100 Subject: [PATCH] added a linkedMap() function to help create LinkedHashMap objects to maintain map insertion order --- libraries/stdlib/src/kotlin/JUtil.kt | 23 ++++++++++++++++++++++- libraries/stdlib/test/MapTest.kt | 18 ++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/libraries/stdlib/src/kotlin/JUtil.kt b/libraries/stdlib/src/kotlin/JUtil.kt index 2b0b5e39b5c..e744fd26494 100644 --- a/libraries/stdlib/src/kotlin/JUtil.kt +++ b/libraries/stdlib/src/kotlin/JUtil.kt @@ -30,7 +30,7 @@ public inline fun sortedSet(vararg values: T) : TreeSet = values.to(TreeSe * @includeFunctionBody ../../test/MapTest.kt createUsingTuples */ public inline fun hashMap(vararg values: #(K,V)): HashMap { - val answer = HashMap() + val answer = HashMap(values.size) /** TODO replace by this simpler call when we can pass vararg values into other methods answer.putAll(values) @@ -44,6 +44,8 @@ public inline fun hashMap(vararg values: #(K,V)): HashMap { /** * Returns a new [[SortedMap]] populated with the given tuple values where the first value in each tuple * is the key and the second value is the value + * + * @includeFunctionBody ../../test/MapTest.kt createSortedMap */ public inline fun sortedMap(vararg values: #(K,V)): SortedMap { val answer = TreeMap() @@ -57,6 +59,25 @@ public inline fun sortedMap(vararg values: #(K,V)): SortedMap { return answer } +/** + * Returns a new [[LinkedHashMap]] populated with the given tuple values where the first value in each tuple + * is the key and the second value is the value. This map preserves insertion order so iterating through + * the map's entries will be in the same order + * + * @includeFunctionBody ../../test/MapTest.kt createLinkedHashMap + */ +public inline fun linkedMap(vararg values: #(K,V)): LinkedHashMap { + val answer = LinkedHashMap(values.size) + /** + TODO replace by this simpler call when we can pass vararg values into other methods + answer.putAll(values) + */ + for (v in values) { + answer.put(v._1, v._2) + } + return answer +} + val Collection<*>.indices : IntRange get() = 0..size-1 diff --git a/libraries/stdlib/test/MapTest.kt b/libraries/stdlib/test/MapTest.kt index 1bb2115e556..9b149f2d370 100644 --- a/libraries/stdlib/test/MapTest.kt +++ b/libraries/stdlib/test/MapTest.kt @@ -119,6 +119,24 @@ class MapTest { assertEquals(2, map.get("b")) } + test fun createLinkedMap() { + val map = linkedMap(#("c", 3), #("b", 2), #("a", 1)) + assertEquals(3, map.size) + assertEquals(1, map.get("a")) + assertEquals(2, map.get("b")) + assertEquals(3, map.get("c")) + assertEquals(arrayList("c", "b", "a"), map.keySet().toList()) + } + + test fun createSortedMap() { + val map = sortedMap(#("c", 3), #("b", 2), #("a", 1)) + assertEquals(3, map.size) + assertEquals(1, map.get("a")) + assertEquals(2, map.get("b")) + assertEquals(3, map.get("c")) + assertEquals(arrayList("a", "b", "c"), map.keySet()!!.toList()) + } + /** TODO test case for http://youtrack.jetbrains.com/issue/KT-1773