Remove empty values from a List of strings in Kotlin
This article explores different ways to remove empty values from a List of strings in Kotlin.
1. Using removeAll() function
The removeAll() function remove all values from the list that are contained in the specified collection. To remove all null or empty strings from a list of strings, you can do like below:
|
1 2 3 4 5 6 7 8 |
fun main() { val values = mutableListOf("A", null, "B", null, "C", "", "D") values.removeAll(listOf("", null)) println(values) // [A, B, C, D] } |
2. Using removeIf() function
Alternatively, you can also use the removeIf() function to remove all items from a list that satisfies the given predicate. A typical invocation for this function would look like:
|
1 2 3 4 5 6 7 |
fun main() { val values = mutableListOf("A", null, "B", null, "C", "", "D") values.removeIf { it == null || it == "" } println(values) // [A, B, C, D] } |
3. Using filter() function
To create a “copy” of the list without changing the original list, you can use filter the list by applying a specified predicate to each element. The following solution returns a list containing all elements that are not null with the filterNotNull() function.
|
1 2 3 4 5 6 7 8 9 |
fun main() { val values = listOf("A", null, "B", null, "C", "", "D") val filteredList = values .filterNotNull() .toList() println(filteredList) // [A, B, C, , D] } |
To remove both null and empty values, you can use the isNullOrEmpty() function, which returns true if the string is either null or empty. The following code example shows invocation for this method using the filter() function. Note that an element is removed only when the predicate returns false. To remove an element only when the predicate returns true, use the filterNot() function.
|
1 2 3 4 5 6 7 8 9 |
fun main() { val values = listOf("A", null, "B", null, "C", "", "D") val filteredList = values .filter { !it.isNullOrEmpty() } // .filterNot { it.isNullOrEmpty() } .toList() println(filteredList) // [A, B, C, D] } |
4. Using Iterator
Finally, you can traverse through the list using a loop and remove all elements that are either null or equal to an empty string. However, it might throw a java.util.ConcurrentModificationException if the List’s remove() function is used. This is because the concurrent modification of the list is not allowed while iterating over it, except by the iterator’s own remove() function method.
This can be implemented as follows in Kotlin using Iterator’s remove() function.
|
1 2 3 4 5 6 7 8 9 10 11 |
fun main() { val values = mutableListOf("A", null, "B", null, "C", "", "D") val i = values.iterator() while (i.hasNext()) { val s = i.next() if (s == null || s.isEmpty()) { i.remove() } } println(values) // [A, B, C, D] } |
That’s all about removing empty values from a List of strings 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 :)