The filterNotNull function ignores all null values and returns a new list without null elements. Let's create a list of different data types, including null values, and then apply the filterNotNull() function:
val list = listOf("One", 2, 3, null, "Four", null)
var newList = list.filterNotNull()
println(newList)
The output of the list is as follows:
[One, 2, 3, Four]
The filterNotNull function can be used instead of the listOfNotNull function, which is used to create a list of non-null elements:
var notNullList = listOfNotNull(1,2,null,"Three","Four")
println("Not null $notNullList")
Here, any null value that is inserted is ignored.