This article explores different ways to remove null, empty, and blank values from a list in Kotlin.

1. Using Filters

A simple solution is to use filters to remove null, empty, and blank values from a list in Kotlin. The filter() function retains elements that match with the specified predicate, and the filterNot() function removes elements that match with the specified predicate.

To remove only null values from a list, we can either use the filterNotNull() function or the mapNotNull() function, as shown below:

Download Code

 
To remove both null and empty values from a list, the recommended approach is to use the filterNot() function with the isNullOrEmpty() function. There are several other options available to remove null and empty values from the list, as demonstrated below:

Download Code

 
To remove null, empty, and blank values from a list, we can use the filterNot() function with the isNullOrBlank() function. The following code demonstrates its usage, along with a couple of other options:

Download Code

2. Using removeIf() function

The above solution creates a copy of the original list. We can in-place remove null, empty, and blank values from the original list using the removeIf() function. The following code example demonstrates the typical invocation of the removeIf() function, to remove all elements that satisfy the specified predicate.

Download Code

Output:

[A, B, , C, D, , E, ]
[A, B, C, D, E, ]
[A, B, C, D, E]

That’s all about removing null, empty, and blank values from a list in Kotlin.