The syntax of Map.isNotEmpty() extension function is:
fun <K, V> Map<out K, V>.isNotEmpty(): Boolean
This isNotEmpty() extension function of Map returns true if this map is not empty.
In this example,
map1
containing key-value pairs.isNotEmpty()
function to check if map1
is not empty.map1
contains elements, true
is printed to standard output.fun main(args: Array<String>) {
val map1 = mapOf("a" to 1, "b" to 2, "c" to 3)
println(map1.isNotEmpty())
}
true
In this example,
map2
.isNotEmpty()
function to check if map2
is not empty.map2
does not contain any elements, false
is printed to standard output.fun main(args: Array<String>) {
val map2 = mapOf<Int, Char>()
println(map2.isNotEmpty())
}
false
In this example,
map3
containing key-value pairs.isNotEmpty()
function to check if map3
is not empty.map3
contains elements, true
is printed to standard output.fun main(args: Array<String>) {
val map3 = mapOf(1 to "apple", 2 to "banana", 3 to "cherry")
println(map3.isNotEmpty())
}
true
In this Kotlin tutorial, we learned about isNotEmpty() extension function of Map: the syntax and few working examples with output and detailed explanation for each example.