To check if a list is not empty in Kotlin, you can use the isNotEmpty()
method of the List
class.
In this example,
myList
with some elements.myList
is not empty by calling the isNotEmpty()
method.fun main() {
val myList = listOf(1, 2, 3)
if (myList.isNotEmpty()) {
println("The list is not empty")
} else {
println("The list is empty")
}
}
The list is not empty
In this example,
emptyList
without any elements.emptyList
is not empty by calling the isNotEmpty()
method.fun main() {
val emptyList = listOf<Int>()
if (emptyList.isNotEmpty()) {
println("The list is not empty")
} else {
println("The list is empty")
}
}
The list is empty
In this tutorial, we learned How to check if a List is Not Empty in Kotlin language with well detailed examples.