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