diff --git a/libraries/stdlib/src/generated/_Mapping.kt b/libraries/stdlib/src/generated/_Mapping.kt index ab4de4fc0a6..b628312aee9 100644 --- a/libraries/stdlib/src/generated/_Mapping.kt +++ b/libraries/stdlib/src/generated/_Mapping.kt @@ -245,84 +245,84 @@ public inline fun > Stream.flatMapTo(destin * Returns a map of the elements in original collection grouped by the result of given *toKey* function */ public inline fun Array.groupBy(toKey: (T) -> K): Map> { - return groupByTo(HashMap>(), toKey) + return groupByTo(LinkedHashMap>(), toKey) } /** * Returns a map of the elements in original collection grouped by the result of given *toKey* function */ public inline fun BooleanArray.groupBy(toKey: (Boolean) -> K): Map> { - return groupByTo(HashMap>(), toKey) + return groupByTo(LinkedHashMap>(), toKey) } /** * Returns a map of the elements in original collection grouped by the result of given *toKey* function */ public inline fun ByteArray.groupBy(toKey: (Byte) -> K): Map> { - return groupByTo(HashMap>(), toKey) + return groupByTo(LinkedHashMap>(), toKey) } /** * Returns a map of the elements in original collection grouped by the result of given *toKey* function */ public inline fun CharArray.groupBy(toKey: (Char) -> K): Map> { - return groupByTo(HashMap>(), toKey) + return groupByTo(LinkedHashMap>(), toKey) } /** * Returns a map of the elements in original collection grouped by the result of given *toKey* function */ public inline fun DoubleArray.groupBy(toKey: (Double) -> K): Map> { - return groupByTo(HashMap>(), toKey) + return groupByTo(LinkedHashMap>(), toKey) } /** * Returns a map of the elements in original collection grouped by the result of given *toKey* function */ public inline fun FloatArray.groupBy(toKey: (Float) -> K): Map> { - return groupByTo(HashMap>(), toKey) + return groupByTo(LinkedHashMap>(), toKey) } /** * Returns a map of the elements in original collection grouped by the result of given *toKey* function */ public inline fun IntArray.groupBy(toKey: (Int) -> K): Map> { - return groupByTo(HashMap>(), toKey) + return groupByTo(LinkedHashMap>(), toKey) } /** * Returns a map of the elements in original collection grouped by the result of given *toKey* function */ public inline fun LongArray.groupBy(toKey: (Long) -> K): Map> { - return groupByTo(HashMap>(), toKey) + return groupByTo(LinkedHashMap>(), toKey) } /** * Returns a map of the elements in original collection grouped by the result of given *toKey* function */ public inline fun ShortArray.groupBy(toKey: (Short) -> K): Map> { - return groupByTo(HashMap>(), toKey) + return groupByTo(LinkedHashMap>(), toKey) } /** * Returns a map of the elements in original collection grouped by the result of given *toKey* function */ public inline fun Iterable.groupBy(toKey: (T) -> K): Map> { - return groupByTo(HashMap>(), toKey) + return groupByTo(LinkedHashMap>(), toKey) } /** * Returns a map of the elements in original collection grouped by the result of given *toKey* function */ public inline fun Stream.groupBy(toKey: (T) -> K): Map> { - return groupByTo(HashMap>(), toKey) + return groupByTo(LinkedHashMap>(), toKey) } /** * Returns a map of the elements in original collection grouped by the result of given *toKey* function */ public inline fun String.groupBy(toKey: (Char) -> K): Map> { - return groupByTo(HashMap>(), toKey) + return groupByTo(LinkedHashMap>(), toKey) } /** diff --git a/libraries/stdlib/src/generated/_Snapshots.kt b/libraries/stdlib/src/generated/_Snapshots.kt index 8692019f98d..5fcf852e08d 100644 --- a/libraries/stdlib/src/generated/_Snapshots.kt +++ b/libraries/stdlib/src/generated/_Snapshots.kt @@ -511,7 +511,7 @@ public fun String.toList(): List { * Returns Map containing all the values from the given collection indexed by *selector* */ public inline fun Array.toMap(selector: (T) -> K): Map { - val result = HashMap() + val result = LinkedHashMap() for (element in this) { result.put(selector(element), element) } @@ -522,7 +522,7 @@ public inline fun Array.toMap(selector: (T) -> K): Map { * Returns Map containing all the values from the given collection indexed by *selector* */ public inline fun BooleanArray.toMap(selector: (Boolean) -> K): Map { - val result = HashMap() + val result = LinkedHashMap() for (element in this) { result.put(selector(element), element) } @@ -533,7 +533,7 @@ public inline fun BooleanArray.toMap(selector: (Boolean) -> K): Map ByteArray.toMap(selector: (Byte) -> K): Map { - val result = HashMap() + val result = LinkedHashMap() for (element in this) { result.put(selector(element), element) } @@ -544,7 +544,7 @@ public inline fun ByteArray.toMap(selector: (Byte) -> K): Map { * Returns Map containing all the values from the given collection indexed by *selector* */ public inline fun CharArray.toMap(selector: (Char) -> K): Map { - val result = HashMap() + val result = LinkedHashMap() for (element in this) { result.put(selector(element), element) } @@ -555,7 +555,7 @@ public inline fun CharArray.toMap(selector: (Char) -> K): Map { * Returns Map containing all the values from the given collection indexed by *selector* */ public inline fun DoubleArray.toMap(selector: (Double) -> K): Map { - val result = HashMap() + val result = LinkedHashMap() for (element in this) { result.put(selector(element), element) } @@ -566,7 +566,7 @@ public inline fun DoubleArray.toMap(selector: (Double) -> K): Map * Returns Map containing all the values from the given collection indexed by *selector* */ public inline fun FloatArray.toMap(selector: (Float) -> K): Map { - val result = HashMap() + val result = LinkedHashMap() for (element in this) { result.put(selector(element), element) } @@ -577,7 +577,7 @@ public inline fun FloatArray.toMap(selector: (Float) -> K): Map { * Returns Map containing all the values from the given collection indexed by *selector* */ public inline fun IntArray.toMap(selector: (Int) -> K): Map { - val result = HashMap() + val result = LinkedHashMap() for (element in this) { result.put(selector(element), element) } @@ -588,7 +588,7 @@ public inline fun IntArray.toMap(selector: (Int) -> K): Map { * Returns Map containing all the values from the given collection indexed by *selector* */ public inline fun LongArray.toMap(selector: (Long) -> K): Map { - val result = HashMap() + val result = LinkedHashMap() for (element in this) { result.put(selector(element), element) } @@ -599,7 +599,7 @@ public inline fun LongArray.toMap(selector: (Long) -> K): Map { * Returns Map containing all the values from the given collection indexed by *selector* */ public inline fun ShortArray.toMap(selector: (Short) -> K): Map { - val result = HashMap() + val result = LinkedHashMap() for (element in this) { result.put(selector(element), element) } @@ -610,7 +610,7 @@ public inline fun ShortArray.toMap(selector: (Short) -> K): Map { * Returns Map containing all the values from the given collection indexed by *selector* */ public inline fun Iterable.toMap(selector: (T) -> K): Map { - val result = HashMap() + val result = LinkedHashMap() for (element in this) { result.put(selector(element), element) } @@ -621,7 +621,7 @@ public inline fun Iterable.toMap(selector: (T) -> K): Map { * Returns Map containing all the values from the given collection indexed by *selector* */ public inline fun Stream.toMap(selector: (T) -> K): Map { - val result = HashMap() + val result = LinkedHashMap() for (element in this) { result.put(selector(element), element) } @@ -632,7 +632,7 @@ public inline fun Stream.toMap(selector: (T) -> K): Map { * Returns Map containing all the values from the given collection indexed by *selector* */ public inline fun String.toMap(selector: (Char) -> K): Map { - val result = HashMap() + val result = LinkedHashMap() for (element in this) { result.put(selector(element), element) } diff --git a/libraries/stdlib/src/kotlin/collections/JUtil.kt b/libraries/stdlib/src/kotlin/collections/JUtil.kt index b5025cd45c5..05bb42dc830 100644 --- a/libraries/stdlib/src/kotlin/collections/JUtil.kt +++ b/libraries/stdlib/src/kotlin/collections/JUtil.kt @@ -17,7 +17,7 @@ public fun listOf(vararg values: T): List = if (values.size == 0) stdlib_e public fun listOf(): List = stdlib_emptyList() /** Returns a new read-only map of given pairs, where the first value is the key, and the second is value */ -public fun mapOf(vararg values: Pair): Map = if (values.size == 0) stdlib_emptyMap() else hashMapOf(*values) +public fun mapOf(vararg values: Pair): Map = if (values.size == 0) stdlib_emptyMap() else linkedMapOf(*values) /** Returns an empty read-only map */ public fun mapOf(): Map = stdlib_emptyMap() @@ -40,6 +40,19 @@ public fun hashMapOf(vararg values: Pair): HashMap { return answer } +/** + * Returns a new [[LinkedHashMap]] populated with the given pairs where the first value in each pair + * 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/collections/MapTest.kt createLinkedMap + */ +public fun linkedMapOf(vararg values: Pair): LinkedHashMap { + val answer = LinkedHashMap(values.size) + answer.putAll(*values) + return answer +} + /** Returns the size of the collection */ public val Collection<*>.size: Int get() = size() diff --git a/libraries/stdlib/src/kotlin/collections/JUtilJVM.kt b/libraries/stdlib/src/kotlin/collections/JUtilJVM.kt index 27fb8175976..a83d1ede0c6 100644 --- a/libraries/stdlib/src/kotlin/collections/JUtilJVM.kt +++ b/libraries/stdlib/src/kotlin/collections/JUtilJVM.kt @@ -36,25 +36,6 @@ public fun sortedMapOf(vararg values: Pair): SortedMap { return answer } -/** - * Returns a new [[LinkedHashMap]] populated with the given pairs where the first value in each pair - * 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/collections/MapTest.kt createLinkedMap - */ -public fun linkedMapOf(vararg values: Pair): 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.first, v.second) - } - return answer -} - /** Returns the Set if its not null otherwise returns the empty set */ public fun Set?.orEmpty(): Set = if (this != null) this else Collections.EMPTY_SET as Set diff --git a/libraries/stdlib/src/kotlin/collections/Maps.kt b/libraries/stdlib/src/kotlin/collections/Maps.kt index 57dcfc176dd..20494f02727 100644 --- a/libraries/stdlib/src/kotlin/collections/Maps.kt +++ b/libraries/stdlib/src/kotlin/collections/Maps.kt @@ -1,7 +1,6 @@ package kotlin -import java.util.Collections -import java.util.HashMap +import java.util.* // Map APIs @@ -129,7 +128,7 @@ public fun MutableMap.putAll(values: Iterable>): Unit { * @includeFunctionBody ../../test/collections/MapTest.kt mapValues */ public inline fun Map.mapValues(transform: (Map.Entry) -> R): Map { - return mapValuesTo(HashMap(this.size), transform) + return mapValuesTo(LinkedHashMap(this.size), transform) } /** @@ -138,14 +137,14 @@ public inline fun Map.mapValues(transform: (Map.Entry) -> * @includeFunctionBody ../../test/collections/MapTest.kt mapKeys */ public inline fun Map.mapKeys(transform: (Map.Entry) -> R): Map { - return mapKeysTo(HashMap(this.size), transform) + return mapKeysTo(LinkedHashMap(this.size), transform) } /** * Returns a map containing all key-value pairs matching keys with the given *predicate* */ public inline fun Map.filterKeys(predicate: (K) -> Boolean): Map { - val result = HashMap() + val result = LinkedHashMap() for (entry in this) { if (predicate(entry.key)) { result.put(entry.key, entry.value) @@ -158,7 +157,7 @@ public inline fun Map.filterKeys(predicate: (K) -> Boolean): Map Map.filterValues(predicate: (V) -> Boolean): Map { - val result = HashMap() + val result = LinkedHashMap() for (entry in this) { if (predicate(entry.value)) { result.put(entry.key, entry.value) @@ -184,7 +183,7 @@ public inline fun > Map.filterTo(destination: C * Returns a map containing all key-value pairs matching the given *predicate* */ public inline fun Map.filter(predicate: (Map.Entry) -> Boolean): Map { - return filterTo(HashMap(), predicate) + return filterTo(LinkedHashMap(), predicate) } /** @@ -203,7 +202,7 @@ public inline fun > Map.filterNotTo(destination * Returns a map containing all key-value pairs matching the given *predicate* */ public inline fun Map.filterNot(predicate: (Map.Entry) -> Boolean): Map { - return filterNotTo(HashMap(), predicate) + return filterNotTo(LinkedHashMap(), predicate) } /** @@ -217,7 +216,7 @@ public fun MutableMap.plusAssign(pair: Pair) { * Returns a map containing all key-value pairs from the given collection */ public fun Iterable>.toMap(): Map { - val result = HashMap() + val result = LinkedHashMap() for (element in this) { result.put(element.first, element.second) } diff --git a/libraries/stdlib/test/collections/MapTest.kt b/libraries/stdlib/test/collections/MapTest.kt index 6ba80d81c45..c1da9679689 100644 --- a/libraries/stdlib/test/collections/MapTest.kt +++ b/libraries/stdlib/test/collections/MapTest.kt @@ -1,14 +1,12 @@ package test.collections import kotlin.test.* - -import java.util.* import org.junit.Test as test class MapTest { test fun getOrElse() { - val data = hashMapOf() + val data = mapOf() val a = data.getOrElse("foo") { 2 } assertEquals(2, a) @@ -16,7 +14,7 @@ class MapTest { assertEquals(3, b) assertEquals(0, data.size()) - val empty = hashMapOf() + val empty = mapOf() val c = empty.getOrElse("") { null } assertEquals(null, c) } @@ -91,7 +89,7 @@ class MapTest { } test fun contains() { - val map = hashMapOf("a" to 1, "b" to 2) + val map = mapOf("a" to 1, "b" to 2) assert("a" in map) assert("c" !in map) } @@ -121,7 +119,7 @@ class MapTest { } test fun createUsingPairs() { - val map = hashMapOf(Pair("a", 1), Pair("b", 2)) + val map = mapOf(Pair("a", 1), Pair("b", 2)) assertEquals(2, map.size) assertEquals(1, map["a"]) assertEquals(2, map["b"]) @@ -135,7 +133,7 @@ class MapTest { } test fun createUsingTo() { - val map = hashMapOf("a" to 1, "b" to 2) + val map = mapOf("a" to 1, "b" to 2) assertEquals(2, map.size) assertEquals(1, map["a"]) assertEquals(2, map["b"]) @@ -158,8 +156,8 @@ class MapTest { } test fun toSortedMap() { - val map = hashMapOf(Pair("c", 3), Pair("b", 2), Pair("a", 1)) - val sorted = map.toSortedMap() + val map = mapOf(Pair("c", 3), Pair("b", 2), Pair("a", 1)) + val sorted = map.toSortedMap() assertEquals(1, sorted["a"]) assertEquals(2, sorted["b"]) assertEquals(3, sorted["c"]) @@ -167,7 +165,7 @@ class MapTest { } test fun toSortedMapWithComparator() { - val map = hashMapOf(Pair("c", 3), Pair("bc", 2), Pair("bd", 4), Pair("abc", 1)) + val map = mapOf(Pair("c", 3), Pair("bc", 2), Pair("bd", 4), Pair("abc", 1)) val c = comparator{ a, b -> val answer = a.length() - b.length() if (answer == 0) a.compareTo(b) else answer @@ -180,7 +178,7 @@ class MapTest { } test fun toProperties() { - val map = hashMapOf("a" to "A", "b" to "B") + val map = mapOf("a" to "A", "b" to "B") val prop = map.toProperties() assertEquals(2, prop.size) assertEquals("A", prop.getProperty("a", "fail")) diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt index 18f2438deb5..b9d2095f3b5 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt @@ -184,7 +184,7 @@ fun mapping(): List { doc { "Returns a map of the elements in original collection grouped by the result of given *toKey* function" } typeParam("K") returns("Map>") - body { "return groupByTo(HashMap>(), toKey)" } + body { "return groupByTo(LinkedHashMap>(), toKey)" } } templates add f("groupByTo(map: MutableMap>, toKey: (T) -> K)") { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt index 0432bdbd2b7..4692853aa65 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt @@ -117,7 +117,7 @@ fun snapshots(): List { returns("Map") body { """ - val result = HashMap() + val result = LinkedHashMap() for (element in this) { result.put(selector(element), element) }