Kotlin List isEmpty()
Syntax & Examples
Syntax of List.isEmpty()
The syntax of List.isEmpty() function is:
abstract fun isEmpty(): Boolean
This 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
list1
containing the elements'a', 'b', 'c'
. - We check if
list1
is empty using the ListisEmpty()
function, which returnstrue
if the list has no elements. - Since
list1
has elements,isEmpty()
returnsfalse
. - Finally, we print the value of
emptyCheck
to standard output using theprintln
function.
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
list2
is empty using the ListisEmpty()
function, which returnstrue
if the list has no elements. - Since
list2
has no elements,isEmpty()
returnstrue
. - Finally, we print the value of
emptyCheck2
to standard output using theprintln
function.
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.