Kotlin List isNullOrEmpty()
Syntax & Examples
Syntax of List.isNullOrEmpty()
The syntax of List.isNullOrEmpty() extension function is:
fun <T> Collection<T>?.isNullOrEmpty(): BooleanThis 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
list1containing the characters'a', 'p', 'p', 'l', 'e'. - We use the
isNullOrEmptyfunction to check iflist1is either null or empty. - Since
list1is not null and contains elements, the expressionlist1.isNullOrEmpty()returnsfalse. - Finally, we print the value of
resultto 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
list1containing the integers10, 20, 30. - We use the
isNullOrEmptyfunction to check iflist1is either null or empty. - Since
list1is not null and contains elements, the expressionlist1.isNullOrEmpty()returnsfalse. - Finally, we print the value of
resultto 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
list1and assign it a null value. - We use the
isNullOrEmptyfunction to check iflist1is either null or empty. - Since
list1is null, the expressionlist1.isNullOrEmpty()returnstrue. - Finally, we print the value of
resultto 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.