Kotlin List isNotEmpty()
Syntax & Examples
Syntax of List.isNotEmpty()
The syntax of List.isNotEmpty() extension function is:
fun <T> Collection<T>.isNotEmpty(): Boolean
This 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
list1
containing elements"apple", "banana", "cherry"
. - We check if
list1
is not empty using theisNotEmpty()
function. - Since
list1
is 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
list2
containing elements10, 20, 30
. - We check if
list2
is not empty using theisNotEmpty()
function. - Since
list2
is 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
list3
containing elements"red", "blue", "green"
. - We check if
list3
is not empty using theisNotEmpty()
function. - Since
list3
is 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.