Remove null and empty values from a list in Kotlin
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:
|
1 2 3 4 5 6 7 8 9 |
fun main() { val input = listOf("A", "B", null, "", "C", "D", null, "", "E", " ") // each println statement outputs [A, B, , C, D, , E, ] println(input.filter { it != null }) println(input.filterNotNull()) println(input.mapNotNull { it }) } |
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:
|
1 2 3 4 5 6 7 8 9 10 |
fun main() { val input = listOf("A", "B", null, "", "C", "D", null, "", "E", " ") // each println statement outputs [A, B, C, D, E, ] println(input.filter { it != null && it.isNotEmpty() }) println(input.filterNotNull().filterNot { it.isEmpty() }) println(input.filterNotNull().filter { it.isNotEmpty() }) println(input.filterNot { it.isNullOrEmpty() }) } |
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:
|
1 2 3 4 5 6 7 8 9 10 |
fun main() { val input = listOf("A", "B", null, "", "C", "D", null, "", "E", " ") // each println statement outputs [A, B, C, D, E] println(input.filter { it != null && !it.isBlank() }) println(input.filterNotNull().filterNot { it.isBlank() }) println(input.filterNotNull().filter { it.isNotBlank() }) println(input.filterNot { it.isNullOrBlank() }) } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
fun main() { val input: MutableList<String?> = mutableListOf( "A", "B", null, "", "C", "D", null, "", "E", " " ) // remove null values input.removeIf { it == null } println(input) // [A, B, , C, D, , E, ] // remove empty values input.removeIf { it!!.isEmpty() } println(input) // [A, B, C, D, E, ] // remove blank values input.removeIf { it!!.isBlank() } println(input) // [A, B, C, D, E] } |
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.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)