diff --git a/libraries/stdlib/src/kotlin/collections/Maps.kt b/libraries/stdlib/src/kotlin/collections/Maps.kt index 530c873489d..75c05a83c47 100644 --- a/libraries/stdlib/src/kotlin/collections/Maps.kt +++ b/libraries/stdlib/src/kotlin/collections/Maps.kt @@ -390,37 +390,38 @@ public fun Sequence>.toMap(): Map /** * Converts this [Map] to a [LinkedHashMap], maintaining the insertion order of elements added to that map afterwards. */ +@Deprecated("It may be too late to convert map this map to linked map, if you care about the order use the ordered map from the beginning.", ReplaceWith("LinkedHashMap(this)", "java.util.LinkedHashMap")) public fun Map.toLinkedMap(): MutableMap = LinkedHashMap(this) /** * Creates a new read-only map by replacing or adding an entry to this map from a given key-value [pair]. */ public operator fun Map.plus(pair: Pair): Map - = this.toLinkedMap().apply { put(pair.first, pair.second) } + = LinkedHashMap(this).apply { put(pair.first, pair.second) } /** * Creates a new read-only map by replacing or adding entries to this map from a given collection of key-value [pairs]. */ public operator fun Map.plus(pairs: Iterable>): Map - = this.toLinkedMap().apply { putAll(pairs) } + = LinkedHashMap(this).apply { putAll(pairs) } /** * Creates a new read-only map by replacing or adding entries to this map from a given array of key-value [pairs]. */ public operator fun Map.plus(pairs: Array>): Map - = this.toLinkedMap().apply { putAll(pairs) } + = LinkedHashMap(this).apply { putAll(pairs) } /** * Creates a new read-only map by replacing or adding entries to this map from a given sequence of key-value [pairs]. */ public operator fun Map.plus(pairs: Sequence>): Map - = this.toLinkedMap().apply { putAll(pairs) } + = LinkedHashMap(this).apply { putAll(pairs) } /** * Creates a new read-only map by replacing or adding entries to this map from another [map]. */ public operator fun Map.plus(map: Map): Map - = this.toLinkedMap().apply { putAll(map) } + = LinkedHashMap(this).apply { putAll(map) } /** diff --git a/libraries/stdlib/test/collections/MapJVMTest.kt b/libraries/stdlib/test/collections/MapJVMTest.kt index 1b59e9fb120..9a4141692f2 100644 --- a/libraries/stdlib/test/collections/MapJVMTest.kt +++ b/libraries/stdlib/test/collections/MapJVMTest.kt @@ -44,7 +44,7 @@ class MapJVMTest { } @test fun iterateAndRemove() { - val map = (1..5).associateBy({ it }, { 'a' + it }).toLinkedMap() + val map = (1..5).associateBy({ it }, { 'a' + it }).toLinkedMap() // TODO: use associateByTo(linkedMapOf()) val iterator = map.iterator() while (iterator.hasNext()) { if (iterator.next().key % 2 == 0)