There are 2 variations for the syntax of Map.none() extension function. They are:
fun <K, V> Map<out K, V>.none(): Boolean
This extension function returns true if the map has no entries.
fun <K, V> Map<out K, V>.none( predicate: (Entry<K, V>) -> Boolean ): Boolean
This extension function returns true if no entries match the given predicate.
In this example,
map1
containing integer keys and character values.none()
function on map1
.true
is returned if the map has no entries.fun main(args: Array<String>) {
val map1 = mapOf(1 to 'a', 2 to 'b', 3 to 'c')
val result = map1.none()
println(result)
}
false
In this example,
map2
containing character keys and integer values.none()
function on map2
with a predicate checking for even values.false
is returned if any even values exist in the map.fun main(args: Array<String>) {
val map2 = mapOf('a' to 1, 'b' to 2, 'c' to 3)
val result = map2.none { it.value % 2 == 0 }
println(result)
}
false
In this example,
map3
containing integer keys and character values.none()
function on map3
with a predicate checking for keys greater than 3.true
is returned if no keys greater than 3 exist in the map.fun main(args: Array<String>) {
val map3 = mapOf(1 to 'a', 2 to 'b', 3 to 'c')
val result = map3.none { it.key > 3 }
println(result)
}
true
In this Kotlin tutorial, we learned about none() extension function of Map: the syntax and few working examples with output and detailed explanation for each example.