Using the 'flatMap' Function for Flattening Collections

The 'flatMap' function is a powerful tool for transforming and flattening collections in Kotlin. It allows you to apply a transformation function to each element of a collection and then flatten the resulting collections into a single collection. This is especially useful when dealing with nested collections or when you want to extract and combine values from multiple sources.

val lists = listOf(listOf(1, 2), listOf(3, 4), listOf(5))
val flattened = lists.flatMap { it } // Result: [1, 2, 3, 4, 5]