From aafd79078f729e01be65aa9c60eebb43a6a4e583 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Sat, 16 Jan 2016 05:49:57 +0300 Subject: [PATCH] Provide mutableSetOf and mutableMapOf #KT-9663 Fixed --- libraries/stdlib/src/kotlin/collections/Maps.kt | 8 ++++++++ libraries/stdlib/src/kotlin/collections/Sets.kt | 2 ++ libraries/stdlib/test/collections/MapTest.kt | 6 ++++++ libraries/stdlib/test/collections/SetOperationsTest.kt | 2 +- 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/libraries/stdlib/src/kotlin/collections/Maps.kt b/libraries/stdlib/src/kotlin/collections/Maps.kt index 123880638bf..f4865943e78 100644 --- a/libraries/stdlib/src/kotlin/collections/Maps.kt +++ b/libraries/stdlib/src/kotlin/collections/Maps.kt @@ -46,6 +46,14 @@ public fun mapOf(): Map = emptyMap() @JvmVersion public fun mapOf(pair: Pair): Map = Collections.singletonMap(pair.first, pair.second) +/** + * Returns a new [MutableMap] with the specified contents, given as a list of pairs + * where the first component is the key and the second is the value. + * This map preserves insertion order so iterating through the map's entries will be in the same order. + */ +public fun mutableMapOf(vararg pairs: Pair): MutableMap + = LinkedHashMap(mapCapacity(pairs.size)).apply { putAll(pairs) } + /** * Returns a new [HashMap] with the specified contents, given as a list of pairs * where the first component is the key and the second is the value. diff --git a/libraries/stdlib/src/kotlin/collections/Sets.kt b/libraries/stdlib/src/kotlin/collections/Sets.kt index b27a0e262fe..f8b177a5cff 100644 --- a/libraries/stdlib/src/kotlin/collections/Sets.kt +++ b/libraries/stdlib/src/kotlin/collections/Sets.kt @@ -31,6 +31,8 @@ public fun setOf(vararg elements: T): Set = if (elements.size > 0) elemen /** Returns an empty read-only set. The returned set is serializable (JVM). */ public fun setOf(): Set = emptySet() +/** Returns a new [MutableSet] with the given elements. */ +public fun mutableSetOf(vararg elements: T): MutableSet = elements.toCollection(LinkedHashSet(mapCapacity(elements.size))) /** Returns a new [HashSet] with the given elements. */ public fun hashSetOf(vararg elements: T): HashSet = elements.toCollection(HashSet(mapCapacity(elements.size))) diff --git a/libraries/stdlib/test/collections/MapTest.kt b/libraries/stdlib/test/collections/MapTest.kt index e3b76d32da7..bdd0bced623 100644 --- a/libraries/stdlib/test/collections/MapTest.kt +++ b/libraries/stdlib/test/collections/MapTest.kt @@ -209,6 +209,12 @@ class MapTest { assertEquals(2, map["b"]) } + @test fun createMutableMap() { + val map = mutableMapOf("b" to 1, "c" to 2) + map.put("a", 3) + assertEquals(listOf("b" to 1, "c" to 2, "a" to 3), map.toList()) + } + @test fun createLinkedMap() { val map = linkedMapOf(Pair("c", 3), Pair("b", 2), Pair("a", 1)) assertEquals(1, map["a"]) diff --git a/libraries/stdlib/test/collections/SetOperationsTest.kt b/libraries/stdlib/test/collections/SetOperationsTest.kt index 2dcbbf479c9..4a1c96b22ab 100644 --- a/libraries/stdlib/test/collections/SetOperationsTest.kt +++ b/libraries/stdlib/test/collections/SetOperationsTest.kt @@ -56,7 +56,7 @@ class SetOperationsTest { assertEquals(setOf("a", "foo", "beer", "cheese", "bar"), set) assertTrue(set !== setOriginal) - val mset = hashSetOf("a") + val mset = mutableSetOf("a") mset += "foo" mset += listOf("beer", "a") mset += arrayOf("cheese", "beer")