To check if a Set is not empty in Kotlin, you can use the isNotEmpty function or compare the size to zero.
In this example,
mySet
containing elements of a specific type.isNotEmpty
function to check if the set is not empty.isNotEmpty
function returns true
, then the set is not empty; otherwise, it is empty.fun main() {
// Define non-empty set
val mySet = setOf("apple", "banana", "orange")
// Check if set is not empty using isNotEmpty function
if (mySet.isNotEmpty()) {
println("Set is not empty")
} else {
println("Set is empty")
}
}
Set is not empty
In this example,
mySet
containing elements of a specific type.size
property to get the number of elements in the set.fun main() {
// Define non-empty set
val mySet = setOf("apple", "banana", "orange")
// Check if set is not empty using size property
if (mySet.size > 0) {
println("Set is not empty")
} else {
println("Set is empty")
}
}
Set is not empty
In this tutorial, we learned How to check if a Set is Not Empty in Kotlin language with well detailed examples.