As we know, Kotlin is very strict when it comes to null objects. For this reason, we are provided with a dedicated list that ignores null objects if they are added. Create a list with the listOfNotNull function and add different variables, including nulls:
fun listOfNonNullObjects(){
val notNulls = listOfNotNull(1,null,"Two",null,'c',4.0,5)
println("Size = ${notNulls.size}")
for (element in notNulls){
println(element)
}
}
If we take a look at the following output:
1, Two, c, 4.0, 5
we can notice that, although we have added seven objects in this list but we got five elements as an output because listOfNotNull does not insert null objects.