The syntax of Map.isEmpty() function is:
abstract fun isEmpty(): Boolean
This isEmpty() function of Map returns true if the map is empty (contains no elements), false otherwise.
In this example,
map1
with keys of type Int
and values of type String
.isEmpty()
function to check if map1
is empty, which it is.true
, is printed to standard output.fun main(args: Array<String>) {
val map1 = emptyMap<Int, String>()
val result = map1.isEmpty()
println(result)
}
true
In this example,
map2
with keys of type Char
and values of type Int
, containing three key-value pairs.isEmpty()
function to check if map2
is empty, which it is not.false
, is printed to standard output.fun main(args: Array<String>) {
val map2 = mapOf('a' to 1, 'b' to 2, 'c' to 3)
val result = map2.isEmpty()
println(result)
}
false
In this example,
map3
with keys of type String
and values of type Int
.isEmpty()
function to check if map3
is empty, which it is.true
, is printed to standard output.fun main(args: Array<String>) {
val map3 = emptyMap<String, Int>()
val result = map3.isEmpty()
println(result)
}
true
In this Kotlin tutorial, we learned about isEmpty() function of Map: the syntax and few working examples with output and detailed explanation for each example.