The filterNotNull function

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.187.210