October 2018
Intermediate to advanced
370 pages
9h 15m
English
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.
Read now
Unlock full access