Kotlin List isNotEmpty()
Syntax & Examples
Syntax of List.isNotEmpty()
The syntax of List.isNotEmpty() extension function is:
fun <T> Collection<T>.isNotEmpty(): BooleanThis isNotEmpty() extension function of List returns true if the collection is not empty.
✐ Examples
1 Example
In this example,
- We create a list of strings named
list1containing elements"apple", "banana", "cherry". - We check if
list1is not empty using theisNotEmpty()function. - Since
list1is not empty, the result istrue. - Finally, we print the result to standard output using println statement.
Kotlin Program
fun main(args: Array<String>) {
val list1 = listOf("apple", "banana", "cherry")
val result = list1.isNotEmpty()
println(result)
}Output
true
2 Example
In this example,
- We create a list of integers named
list2containing elements10, 20, 30. - We check if
list2is not empty using theisNotEmpty()function. - Since
list2is not empty, the result istrue. - Finally, we print the result to standard output using println statement.
Kotlin Program
fun main(args: Array<String>) {
val list2 = listOf(10, 20, 30)
val result = list2.isNotEmpty()
println(result)
}Output
true
3 Example
In this example,
- We create a list of strings named
list3containing elements"red", "blue", "green". - We check if
list3is not empty using theisNotEmpty()function. - Since
list3is not empty, the result istrue. - Finally, we print the result to standard output using println statement.
Kotlin Program
fun main(args: Array<String>) {
val list3 = listOf("red", "blue", "green")
val result = list3.isNotEmpty()
println(result)
}Output
true
Summary
In this Kotlin tutorial, we learned about isNotEmpty() extension function of List: the syntax and few working examples with output and detailed explanation for each example.