The isNotEmpty() extension function in Kotlin returns true if the collection is not empty.
The syntax of Set.isNotEmpty() extension function is:
fun <T> Collection<T>.isNotEmpty(): Boolean
This isNotEmpty() extension function of Set returns true if the collection is not empty.
Set.isNotEmpty() returns value of type Boolean
.
Using isNotEmpty() to check if a set of integers is not empty.
For example,
fun main() {
val numbers = setOf(1, 2, 3)
val result = numbers.isNotEmpty()
println(result)
}
true
Using isNotEmpty() to check if an empty set of strings is not empty.
For example,
fun main() {
val strings = emptySet<String>()
val result = strings.isNotEmpty()
println(result)
}
false
Using isNotEmpty() to check if a set of custom objects is not empty.
For example,
data class Person(val name: String, val age: Int)
fun main() {
val people = setOf(Person("Alice", 30), Person("Bob", 25))
val result = people.isNotEmpty()
println(result)
}
true
In this Kotlin tutorial, we learned about isNotEmpty() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.