The syntax of Map.isNullOrEmpty() extension function is:
fun <K, V> Map<out K, V>?.isNullOrEmpty(): Boolean
This isNullOrEmpty() extension function of Map returns true if this nullable map is either null or empty.
In this example,
map1
.isNullOrEmpty
function on map1
.map1
is non-empty, the function returns false
.fun main(args: Array<String>) {
val map1 = mapOf("key1" to "value1", "key2" to "value2")
val result1 = map1.isNullOrEmpty()
println(result1)
}
false
In this example,
map2
.isNullOrEmpty
function on map2
.map2
is empty, the function returns true
.fun main(args: Array<String>) {
val map2 = emptyMap<String, Int>()
val result2 = map2.isNullOrEmpty()
println(result2)
}
true
In this example,
map3
and assign it a value of null
.isNullOrEmpty
function on map3
.map3
is null
, the function returns true
.fun main(args: Array<String>) {
val map3: Map<String, String>? = null
val result3 = map3.isNullOrEmpty()
println(result3)
}
true
In this Kotlin tutorial, we learned about isNullOrEmpty() extension function of Map: the syntax and few working examples with output and detailed explanation for each example.