The filterIsInstance<dataType> function

This function allows us to filter out elements of specific types. We can create a list of different elements and call the filterIsInstance<type> function by explicitly defining a data type that we want to filter out. Take a look at the following example:

val list = listOf("One", 2.0, 3, null, "Four", 5)

val stringList = list.filterIsInstance<String>()
println("Filter string elements")
println(stringList)

val intList = list.filterIsInstance<Int>()
println("Filter int elements")
println(intList)

The filterIsInstance<String> function will return a list of String elements, while filterIsInstance<Int> will return a list of Int elements.

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

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