This page presents some common tasks and their solutions in relation to collections.

Remove all nulls from a Collection

You want to remove all entries from a collection which are null.


Kotlin offers many useful (extension) functions to filter collections - one of it is called filterNotNull, which returns a new collection containing all entries of the receiving collection but nulls.

val collection = listOf("Hello",null,"World")
val noNulls = collection.filterNotNull()
check(noNulls.count { it==null } == 0)