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 if list1 is either null or empty.
  • Since list1 is not null and contains elements, the expression list1.isNullOrEmpty() returns false.
  • 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 integers 10, 20, 30.
  • We use the isNullOrEmpty function to check if list1 is either null or empty.
  • Since list1 is not null and contains elements, the expression list1.isNullOrEmpty() returns false.
  • 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 if list1 is either null or empty.
  • Since list1 is null, the expression list1.isNullOrEmpty() returns true.
  • 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.