To check if a Set is empty in Kotlin, you can utilize the isEmpty()
method provided by the Set interface.
In this example,
set
with some elements, or an empty Set if there are no elements.isEmpty()
method on the Set set
to determine if it contains any elements.isEmpty()
method returns true
; otherwise, it returns false
.fun main() {
val set = setOf("a", "b", "c") // Or val set = emptySet<String>() for an empty Set
val isEmpty = set.isEmpty()
if (isEmpty) {
println("The Set is empty")
} else {
println("The Set is not empty")
}
}
The Set is not empty
In this example,
emptySet
.isEmpty()
method on the Set emptySet
to determine if it contains any elements.isEmpty()
method returns true
.fun main() {
val emptySet = emptySet<String>()
val isEmpty = emptySet.isEmpty()
if (isEmpty) {
println("The Set is empty")
} else {
println("The Set is not empty")
}
}
The Set is empty
In this tutorial, we learned How to check if a Set is Empty in Kotlin language with well detailed examples.