Kotlin List isNullOrEmpty()
Syntax & Examples
Syntax of List.isNullOrEmpty()
The syntax of List.isNullOrEmpty() extension function is:
fun <T> Collection<T>?.isNullOrEmpty(): Boolean
This isNullOrEmpty() extension function of List returns true if this nullable collection is either null or empty.
✐ Examples
1 Example
In this example,
- We create a nullable list named
list1
containing the characters'a', 'p', 'p', 'l', 'e'
. - We use the
isNullOrEmpty
function to check iflist1
is either null or empty. - Since
list1
is not null and contains elements, the expressionlist1.isNullOrEmpty()
returnsfalse
. - Finally, we print the value of
result
to standard output.
Kotlin Program
fun main(args: Array<String>) {
val list1: List<String>? = listOf("a", "p", "p", "l", "e");
val result = list1.isNullOrEmpty();
println(result);
}
Output
false
2 Example
In this example,
- We create a nullable list named
list1
containing the integers10, 20, 30
. - We use the
isNullOrEmpty
function to check iflist1
is either null or empty. - Since
list1
is not null and contains elements, the expressionlist1.isNullOrEmpty()
returnsfalse
. - Finally, we print the value of
result
to standard output.
Kotlin Program
fun main(args: Array<String>) {
val list1: List<Int>? = listOf(10, 20, 30);
val result = list1.isNullOrEmpty();
println(result);
}
Output
false
3 Example
In this example,
- We create a nullable list named
list1
and assign it a null value. - We use the
isNullOrEmpty
function to check iflist1
is either null or empty. - Since
list1
is null, the expressionlist1.isNullOrEmpty()
returnstrue
. - Finally, we print the value of
result
to standard output.
Kotlin Program
fun main(args: Array<String>) {
val list1: List<String>? = null;
val result = list1.isNullOrEmpty();
println(result);
}
Output
true
Summary
In this Kotlin tutorial, we learned about isNullOrEmpty() extension function of List: the syntax and few working examples with output and detailed explanation for each example.