Kotlin List isEmpty()
Syntax & Examples
Syntax of List.isEmpty()
The syntax of List.isEmpty() function is:
abstract fun isEmpty(): BooleanThis isEmpty() function of List returns true if the collection is empty (contains no elements), false otherwise.
✐ Examples
1 Example
In this example,
- We create a list named
list1containing the elements'a', 'b', 'c'. - We check if
list1is empty using the ListisEmpty()function, which returnstrueif the list has no elements. - Since
list1has elements,isEmpty()returnsfalse. - Finally, we print the value of
emptyCheckto standard output using theprintlnfunction.
Kotlin Program
fun main(args: Array<String>) {
val list1 = listOf("a", "b", "c")
val emptyCheck = list1.isEmpty()
println(emptyCheck)
}Output
false
2 Example
In this example,
- We create an empty list named
list2. - We check if
list2is empty using the ListisEmpty()function, which returnstrueif the list has no elements. - Since
list2has no elements,isEmpty()returnstrue. - Finally, we print the value of
emptyCheck2to standard output using theprintlnfunction.
Kotlin Program
fun main(args: Array<String>) {
val list2 = listOf<Int>()
val emptyCheck2 = list2.isEmpty()
println(emptyCheck2)
}Output
true
Summary
In this Kotlin tutorial, we learned about isEmpty() function of List: the syntax and few working examples with output and detailed explanation for each example.