This post will discuss different ways to check whether a list is empty in Kotlin. A list is empty if and only if it contains no elements.

1. Using isEmpty()/isNotEmpty() function

A simple solution to check for an empty list in Kotlin is using the isEmpty() function. Its usage is demonstrated below:

Download Code

 
Instead of checking separately if the list is null or empty, you can combine the isEmpty() function with the null check:

Download Code

 
Or, do inverse using the isNotEmpty() function:

Download Code

2. Using size property

Another solution is to use the size property or the count() function, both return the number of elements in the collection.

Download Code

That’s all about checking whether a list is empty in Kotlin.