diff --git a/libraries/stdlib/src/kotlin/collections/Maps.kt b/libraries/stdlib/src/kotlin/collections/Maps.kt index 2a694cc2aea..099b350a108 100644 --- a/libraries/stdlib/src/kotlin/collections/Maps.kt +++ b/libraries/stdlib/src/kotlin/collections/Maps.kt @@ -222,6 +222,15 @@ public fun MutableMap.putAll(values: Iterable>): Unit { } } +/** + * Puts all the elements of the given sequence into this [MutableMap] with the first component in the pair being the key and the second the value. + */ +public fun MutableMap.putAll(values: Sequence>): Unit { + for ((key, value) in values) { + put(key, value) + } +} + /** * Returns a new map with entries having the keys of this map and the values obtained by applying the `transform` * function to each entry in this [Map]. @@ -336,6 +345,29 @@ public fun MutableMap.plusAssign(map: Map) { * Returns a new map containing all key-value pairs from the given collection of pairs. */ public fun Iterable>.toMap(): Map { + val result = LinkedHashMap(collectionSizeOrNull()?.let { mapCapacity(it) } ?: 16) + for (element in this) { + result.put(element.first, element.second) + } + return result +} + +/** + * Returns a new map containing all key-value pairs from the given array of pairs. + */ +public fun Array>.toMap(): Map { + val result = LinkedHashMap(mapCapacity(size())) + for (element in this) { + result.put(element.first, element.second) + } + return result +} + +/** + * Returns a new map containing all key-value pairs from the given sequence of pairs. + */ + +public fun Sequence>.toMap(): Map { val result = LinkedHashMap() for (element in this) { result.put(element.first, element.second)