diff --git a/runtime/src/main/kotlin/kotlin/collections/Maps.kt b/runtime/src/main/kotlin/kotlin/collections/Maps.kt index c13953c35f7..6109d7e863f 100644 --- a/runtime/src/main/kotlin/kotlin/collections/Maps.kt +++ b/runtime/src/main/kotlin/kotlin/collections/Maps.kt @@ -61,12 +61,13 @@ public fun mapOf(vararg pairs: Pair): Map = @kotlin.internal.InlineOnly public inline fun mapOf(): Map = emptyMap() +// TODO: Add a singleton map class (see Kotlin JVM mapOf(Pair) implementation). /** * Returns an immutable map, mapping only the specified key to the * specified value. The returned map is serializable. * @sample samples.collections.Maps.Instantiation.mapFromPairs */ -//public fun mapOf(pair: Pair): Map = java.util.Collections.singletonMap(pair.first, pair.second) +public fun mapOf(pair: Pair): Map = hashMapOf(pair) /** * Returns a new [MutableMap] with the specified contents, given as a list of pairs @@ -227,6 +228,18 @@ internal inline fun Map.getOrElseNullable(key: K, defaultValue: () } } +/** + * Returns the value for the given [key] or throws an exception if there is no such key in the map. + * + * If the map was created by [withDefault], resorts to its `defaultValue` provider function + * instead of throwing an exception. + * + * @throws NoSuchElementException when the map doesn't contain a value for the specified key and + * no implicit default value was provided for that map. + */ +@SinceKotlin("1.1") +public fun Map.getValue(key: K): V = getOrImplicitDefault(key) + /** * Returns the value for the given key. If the key is not found in the map, calls the [defaultValue] function, * puts its result into the map under the given key and returns it. @@ -718,3 +731,73 @@ public inline fun Map.count(predicate: (Map.Entry) -> Boo public inline fun Map.forEach(action: (Map.Entry) -> Unit): Unit { for (element in this) action(element) } + +/** + * Returns the first entry yielding the largest value of the given function or `null` if there are no entries. + */ +@kotlin.internal.InlineOnly +public inline fun > Map.maxBy(selector: (Map.Entry) -> R): Map.Entry? { + return entries.maxBy(selector) +} + +/** + * Returns the first entry having the largest value according to the provided [comparator] or `null` if there are no entries. + */ +@kotlin.internal.InlineOnly +public inline fun Map.maxWith(comparator: Comparator>): Map.Entry? { + return entries.maxWith(comparator) +} + +/** + * Returns the first entry yielding the smallest value of the given function or `null` if there are no entries. + */ +public inline fun > Map.minBy(selector: (Map.Entry) -> R): Map.Entry? { + return entries.minBy(selector) +} + +/** + * Returns the first entry having the smallest value according to the provided [comparator] or `null` if there are no entries. + */ +public fun Map.minWith(comparator: Comparator>): Map.Entry? { + return entries.minWith(comparator) +} + +/** + * Returns `true` if the map has no entries. + */ +public fun Map.none(): Boolean { + for (element in this) return false + return true +} + +/** + * Returns `true` if no entries match the given [predicate]. + */ +public inline fun Map.none(predicate: (Map.Entry) -> Boolean): Boolean { + for (element in this) if (predicate(element)) return false + return true +} + +/** + * Performs the given [action] on each entry and returns the map itself afterwards. + */ +@SinceKotlin("1.1") +public inline fun > M.onEach(action: (Map.Entry) -> Unit): M { + return apply { for (element in this) action(element) } +} + +/** + * Creates an [Iterable] instance that wraps the original map returning its entries when being iterated. + */ +@kotlin.internal.InlineOnly +public inline fun Map.asIterable(): Iterable> { + return entries +} + +/** + * Creates a [Sequence] instance that wraps the original map returning its entries when being iterated. + */ +public fun Map.asSequence(): Sequence> { + return entries.asSequence() +} +