The syntax of Map.minOf() extension function is:
fun <K, V> Map<out K, V>.minOf( selector: (Entry<K, V>) -> Double ): Double
This minOf() extension function of Map returns the smallest value among all values produced by selector function applied to each entry in the map.
In this example,
map1
with key-value pairs.minOf()
function on map1
with a selector based on values converted to Double.map1
.fun main(args: Array<String>) {
val map1 = mapOf("key1" to 1, "key2" to 2, "key3" to 3)
val result = map1.minOf { it.value.toDouble() }
println(result)
}
1.0
In this example,
map2
with key-value pairs.minOf()
function on map2
with a selector based on key lengths converted to Double.map2
.fun main(args: Array<String>) {
val map2 = mapOf("apple" to 1, "banana" to 2, "cherry" to 3)
val result = map2.minOf { it.key.length.toDouble() }
println(result)
}
5.0
In this example,
map3
with key-value pairs.minOf()
function on map3
with a selector based on key lengths converted to Double.map3
.fun main(args: Array<String>) {
val map3 = mapOf("one" to 1, "two" to 2, "three" to 3)
val result = map3.minOf { it.key.length.toDouble() }
println(result)
}
3.0
In this Kotlin tutorial, we learned about minOf() extension function of Map: the syntax and few working examples with output and detailed explanation for each example.