Kotlin List.isEmpty()
The Kotlin List.isEmpty() function returns true if the list is empty meaning no elements in the list, false otherwise.
Syntax
List.isEmpty()
Example 1
In this example,
- Take an empty list.
- Call isEmpty() function on the list. As our list is empty, the function should return true.
Program
fun main(args: Array<String>) {
val list1 = listOf<String>()
val result = list1.isEmpty()
print(result)
}
Output
true
Example 2
In this example,
- Take an list with some elements in it.
- Call isEmpty() function on the list. As our list is not empty, the function should return false.
Program
fun main(args: Array<String>) {
val list1 = listOf(25, 41, 20)
val result = list1.isEmpty()
print(result)
}
Output
false