This article explores different ways to check if a List is empty in Kotlin.

1. Using isEmpty() function

The standard solution to check if a list is empty in Kotlin is with the isEmpty() library function. It returns true if the list contains no elements, false otherwise. Following is a simple example demonstrating the usage of this method, which handles null input gracefully.

Download Code

Output:

The list is empty

2. Using isNotEmpty() function

Alternatively, we can check if the list is not empty using the isNotEmpty() function. This is demonstrated below:

Download Code

Output:

The list is empty

3. Checking List of Lists

To check for an empty list or a null value in a List of Lists, we can use the any() function that returns true if at least one element matches the given predicate.

Download Code

Output:

The list contains a null or an empty list

That’s all about checking if a List is empty in Kotlin.