diff --git a/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt b/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt index 5c4f97f5a36..67a2d27ff9d 100644 --- a/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt +++ b/libraries/stdlib/src/kotlin/collections/MapWithDefault.kt @@ -20,30 +20,30 @@ public fun Map.getOrImplicitDefault(key: K): V { } /** - * Returns a wrapper of this read-only map, having the implicit default value provided with the specified function [default]. + * Returns a wrapper of this read-only map, having the implicit default value provided with the specified function [defaultValue]. * This implicit default value is used when [getOrImplicitDefault] is called on the returned map, * and that map doesn't contain value for the key specified. * * When this map already have an implicit default value provided with a former call to [withDefault], it is being replaced by this call. */ -public fun Map.withDefault(default: (key: K) -> V): Map = +public fun Map.withDefault(defaultValue: (key: K) -> V): Map = when (this) { - is MapWithDefault -> this.map.withDefault(default) - else -> MapWithDefaultImpl(this, default) + is MapWithDefault -> this.map.withDefault(defaultValue) + else -> MapWithDefaultImpl(this, defaultValue) } /** - * Returns a wrapper of this mutable map, having the implicit default value provided with the specified function [default]. + * Returns a wrapper of this mutable map, having the implicit default value provided with the specified function [defaultValue]. * This implicit default value is used when [getOrImplicitDefault] is called on the returned map, * and that map doesn't contain value for the key specified. * * When this map already have an implicit default value provided with a former call to [withDefault], it is being replaced by this call. */ @kotlin.jvm.JvmName("withDefaultMutable") -public fun MutableMap.withDefault(default: (key: K) -> V): MutableMap = +public fun MutableMap.withDefault(defaultValue: (key: K) -> V): MutableMap = when (this) { - is MutableMapWithDefault -> this.map.withDefault(default) - else -> MutableMapWithDefaultImpl(this, default) + is MutableMapWithDefault -> this.map.withDefault(defaultValue) + else -> MutableMapWithDefaultImpl(this, defaultValue) } diff --git a/libraries/stdlib/src/kotlin/collections/Maps.kt b/libraries/stdlib/src/kotlin/collections/Maps.kt index 1e85f0619a4..2b0aaec4744 100644 --- a/libraries/stdlib/src/kotlin/collections/Maps.kt +++ b/libraries/stdlib/src/kotlin/collections/Maps.kt @@ -34,7 +34,7 @@ public fun emptyMap(): Map = EmptyMap as Map * * The returned map is serializable (JVM). */ -public fun mapOf(vararg values: Pair): Map = if (values.size > 0) linkedMapOf(*values) else emptyMap() +public fun mapOf(vararg pairs: Pair): Map = if (pairs.size > 0) linkedMapOf(*pairs) else emptyMap() /** Returns an empty read-only map. The returned map is serializable (JVM). */ public fun mapOf(): Map = emptyMap() @@ -44,7 +44,7 @@ public fun mapOf(): Map = emptyMap() * specified value. The returned map is serializable. */ @JvmVersion -public fun mapOf(keyValuePair: Pair): Map = Collections.singletonMap(keyValuePair.first, keyValuePair.second) +public fun mapOf(pair: Pair): Map = Collections.singletonMap(pair.first, pair.second) /** * Returns a new [HashMap] with the specified contents, given as a list of pairs @@ -52,11 +52,9 @@ public fun mapOf(keyValuePair: Pair): Map = Collections.singl * * @sample test.collections.MapTest.createUsingPairs */ -public fun hashMapOf(vararg values: Pair): HashMap { - val answer = HashMap(mapCapacity(values.size)) - answer.putAll(*values) - return answer -} +public fun hashMapOf(vararg pairs: Pair): HashMap + = HashMap(mapCapacity(pairs.size)).apply { putAll(pairs) } + /** * Returns a new [LinkedHashMap] with the specified contents, given as a list of pairs @@ -65,11 +63,8 @@ public fun hashMapOf(vararg values: Pair): HashMap { * * @sample test.collections.MapTest.createLinkedMap */ -public fun linkedMapOf(vararg values: Pair): LinkedHashMap { - val answer = LinkedHashMap(mapCapacity(values.size)) - answer.putAll(*values) - return answer -} +public fun linkedMapOf(vararg pairs: Pair): LinkedHashMap + = LinkedHashMap(mapCapacity(pairs.size)).apply { putAll(pairs) } /** * Calculate the initial capacity of a map, based on Guava's com.google.common.collect.Maps approach. This is equivalent @@ -220,10 +215,19 @@ public inline fun > Map.mapKeysTo(destinatio } /** - * Puts all the given [values] into this [MutableMap] with the first component in the pair being the key and the second the value. + * Puts all the given [pairs] into this [MutableMap] with the first component in the pair being the key and the second the value. */ -public fun MutableMap.putAll(vararg values: Pair): Unit { - for ((key, value) in values) { +@kotlin.jvm.JvmName("putAllVararg") +@Deprecated("Use an overload without vararg", ReplaceWith("putAll(pairs)")) +public fun MutableMap.putAll(vararg pairs: Pair): Unit { + putAll(pairs) +} + +/** + * Puts all the given [pairs] into this [MutableMap] with the first component in the pair being the key and the second the value. + */ +public fun MutableMap.putAll(pairs: Array>): Unit { + for ((key, value) in pairs) { put(key, value) } } @@ -231,8 +235,8 @@ public fun MutableMap.putAll(vararg values: Pair): Unit { /** * Puts all the elements of the given collection into this [MutableMap] with the first component in the pair being the key and the second the value. */ -public fun MutableMap.putAll(values: Iterable>): Unit { - for ((key, value) in values) { +public fun MutableMap.putAll(pairs: Iterable>): Unit { + for ((key, value) in pairs) { put(key, value) } } @@ -240,8 +244,8 @@ 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) { +public fun MutableMap.putAll(pairs: Sequence>): Unit { + for ((key, value) in pairs) { put(key, value) } } @@ -338,36 +342,21 @@ public inline fun Map.filterNot(predicate: (Map.Entry) -> Boo /** * 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 -} +public fun Iterable>.toMap(): Map + = LinkedHashMap(collectionSizeOrNull()?.let { mapCapacity(it) } ?: 16).apply { putAll(this@toMap) } /** * 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 -} +public fun Array>.toMap(): Map + = LinkedHashMap(mapCapacity(size)).apply { putAll(this@toMap) } /** * 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) - } - return result -} +public fun Sequence>.toMap(): Map + = LinkedHashMap().apply { putAll(this@toMap) } /** * Converts this [Map] to a [LinkedHashMap], maintaining the insertion order of elements added to that map afterwards. @@ -377,47 +366,33 @@ 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 { - val newMap = this.toLinkedMap() - newMap.put(pair.first, pair.second) - return newMap -} +public operator fun Map.plus(pair: Pair): Map + = this.toLinkedMap().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 { - val newMap = this.toLinkedMap() - newMap.putAll(pairs) - return newMap -} +public operator fun Map.plus(pairs: Iterable>): Map + = this.toLinkedMap().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 { - val newMap = this.toLinkedMap() - newMap.putAll(*pairs) - return newMap -} +public operator fun Map.plus(pairs: Array>): Map + = this.toLinkedMap().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 { - val newMap = this.toLinkedMap() - newMap.putAll(pairs) - return newMap -} +public operator fun Map.plus(pairs: Sequence>): Map + = this.toLinkedMap().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 { - val newMap = this.toLinkedMap() - newMap.putAll(map) - return newMap -} +public operator fun Map.plus(map: Map): Map + = this.toLinkedMap().apply { putAll(map) } + /** * Appends or replaces the given [pair] in this mutable map. @@ -436,8 +411,8 @@ public operator fun MutableMap.plusAssign(pairs: Iterable MutableMap.plusAssign(pairs: Array>) { - putAll(*pairs) +public operator fun MutableMap.plusAssign(pairs: Array>) { + putAll(pairs) } /** @@ -457,38 +432,26 @@ public operator fun MutableMap.plusAssign(map: Map) { /** * Creates a new read-only map by removing a [key] from this map. */ -public operator fun Map.minus(key: K): Map { - val result = LinkedHashMap(this) - result.minusAssign(key) - return result -} +public operator fun Map.minus(key: K): Map + = this.toLinkedMap().apply { minusAssign(key) } /** * Creates a new read-only map by removing a collection of [keys] from this map. */ -public operator fun Map.minus(keys: Iterable): Map { - val result = LinkedHashMap(this) - result.minusAssign(keys) - return result -} +public operator fun Map.minus(keys: Iterable): Map + = this.toLinkedMap().apply { minusAssign(keys) } /** * Creates a new read-only map by removing a array of [keys] from this map. */ -public operator fun Map.minus(keys: Array): Map { - val result = LinkedHashMap(this) - result.minusAssign(keys) - return result -} +public operator fun Map.minus(keys: Array): Map + = this.toLinkedMap().apply { minusAssign(keys) } /** * Creates a new read-only map by removing a sequence of [keys] from this map. */ -public operator fun Map.minus(keys: Sequence): Map { - val result = LinkedHashMap(this) - result.minusAssign(keys) - return result -} +public operator fun Map.minus(keys: Sequence): Map + = this.toLinkedMap().apply { minusAssign(keys) } /** * Removes the given [key] from this mutable map. diff --git a/libraries/stdlib/src/kotlin/collections/MapsJVM.kt b/libraries/stdlib/src/kotlin/collections/MapsJVM.kt index d05e8e74507..ef2292b6f7c 100644 --- a/libraries/stdlib/src/kotlin/collections/MapsJVM.kt +++ b/libraries/stdlib/src/kotlin/collections/MapsJVM.kt @@ -59,11 +59,8 @@ public fun , V> Map.toSortedMap(): SortedMap = Tre * * @sample test.collections.MapJVMTest.toSortedMapWithComparator */ -public fun Map.toSortedMap(comparator: Comparator): SortedMap { - val result = TreeMap(comparator) - result.putAll(this) - return result -} +public fun Map.toSortedMap(comparator: Comparator): SortedMap + = TreeMap(comparator).apply { putAll(this@toSortedMap) } /** * Returns a new [SortedMap] with the specified contents, given as a list of pairs @@ -71,17 +68,8 @@ public fun Map.toSortedMap(comparator: Comparator): SortedMap * * @sample test.collections.MapJVMTest.createSortedMap */ -public fun sortedMapOf(vararg values: Pair): SortedMap { - val answer = TreeMap() - /** - 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 -} +public fun sortedMapOf(vararg pairs: Pair): SortedMap + = TreeMap().apply { putAll(pairs) } /** @@ -89,11 +77,6 @@ public fun sortedMapOf(vararg values: Pair): SortedMap { * * @sample test.collections.MapJVMTest.toProperties */ -public fun Map.toProperties(): Properties { - val answer = Properties() - for (e in this) { - answer.put(e.key, e.value) - } - return answer -} +public fun Map.toProperties(): Properties + = Properties().apply { putAll(this@toProperties) }