The 'mapNotNull' function is a powerful tool in Kotlin that allows you to transform a collection while filtering out null results in a single operation. This is particularly useful when you want to apply a transformation to a collection, but only keep the non-null results. It helps to keep your code clean and concise by eliminating the need for additional filtering steps after mapping.
val numbers = listOf("1", "2", "three", "4", null, "5") val validNumbers = numbers.mapNotNull { it?.toIntOrNull() } // validNumbers will be [1, 2, 4, 5]