The isEmpty() function of the Set class in Kotlin checks if the set is empty, returning true if it contains no elements and false otherwise.
The syntax of Set.isEmpty() function is:
abstract fun isEmpty(): Boolean
This isEmpty() function of Set returns true if the collection is empty (contains no elements), false otherwise.
Set.isEmpty() returns value of type Boolean
.
In Kotlin, we can use the isEmpty()
function to check if a set is empty.
For example,
isEmpty()
function to check if the set is empty.println
function.fun main(args: Array<String>) {
val emptySet = emptySet<Int>()
val isEmpty = emptySet.isEmpty()
println("Is the set empty? $isEmpty")
}
Is the set empty? true
In Kotlin, we can use the isEmpty()
function to check if a non-empty set contains any elements.
For example,
isEmpty()
function to check if the set is empty.println
function.fun main(args: Array<String>) {
val fruits = setOf("apple", "banana", "cherry")
val isEmpty = fruits.isEmpty()
println("Is the set empty? $isEmpty")
}
Is the set empty? false
In Kotlin, the isEmpty()
function can also be used with mutable sets to check if the set is empty after adding elements.
For example,
isEmpty()
function to check if the set is empty.println
function.fun main(args: Array<String>) {
val mutableSet = mutableSetOf<String>()
mutableSet.add("apple")
val isEmpty = mutableSet.isEmpty()
println("Is the mutable set empty? $isEmpty")
}
Is the mutable set empty? false
In this Kotlin tutorial, we learned about isEmpty() function of Set: the syntax and few working examples with output and detailed explanation for each example.