Remove null values from a list in Kotlin
This article explores different ways to remove null values from a list in Kotlin.
1. Using removeIf() function
The removeIf() function removes all elements of the list that satisfy the given predicate. To remove null values from the list, you can use a lambda expression to find null values.
|
1 2 3 4 5 6 7 |
fun main() { val list: MutableList<String?> = arrayListOf("A", null, "B", null, "C") list.removeIf { obj: String? -> obj == null } println(list) // [A, B, C] } |
2. Using removeAll() function
To remove all occurrences of null values from a list, you can pass a singleton list having only a null value to the removeAll() function.
|
1 2 3 4 5 6 |
fun main() { val list: MutableList<String?> = arrayListOf("A", null, "B", null, "C") list.removeAll(listOf(null)) println(list) } |
3. Using filter() function
You can use the filter() function that returns a new list consisting of the elements matching the specified predicate. You can specify a lambda expression to return a new list with null values removed from the list, as shown below:
|
1 2 3 4 5 6 7 |
fun main() { var list = listOf("A", null, "B", null, "C") list = list.filter { x: String? -> x != null } println(list) // [A, B, C] } |
4. Using filterNotNull() function
A simple and fairly efficient solution is to remove null values from a list to call the filterNotNull() function on the list. Note, this approach creates a new list.
|
1 2 3 4 5 6 7 |
fun main() { var list = listOf("A", null, "B", null, "C") list = list.filterNotNull(); println(list) // [A, B, C] } |
5. Using remove() function
The remove() function removes the first occurrence of the specified object from the list. To remove all null occurrences from the list, you can continuously call remove(null) until all null values are removed from the list.
|
1 2 3 4 5 6 |
fun main() { val list: MutableList<String?> = arrayListOf("A", null, "B", null, "C") while (list.remove(null)) {} println(list) } |
6. Using List Iterator
Alternatively, you can loop over the list using an iterator and remove all null elements from it.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
fun <T> removeNulls(list: MutableList<T>) { val itr = list.iterator() while (itr.hasNext()) { val curr: T? = itr.next() if (curr == null) { itr.remove() // remove nulls } } } fun main() { val list: MutableList<String?> = arrayListOf("A", null, "B", null, "C") removeNulls(list) println(list) } |
7. Map the null values
Instead of removing null values from a list, you can also replace the null values with some value. This approach is demonstrated below:
|
1 2 3 4 5 6 7 |
fun main() { var list = listOf("A", null, "B", null, "C") list = list.map { x: String? -> x ?: "EMPTY" } println(list) // [A, EMPTY, B, EMPTY, C] } |
That’s all about removing null 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 :)