#Lesson 41 : Kotlin map vs. flatMap
fun main(args: Array<String>) { val listofAlphabets = listOf( Alphabet(listOf("A", "B", "C")), Alphabet(listOf("D", "E", "F")), Alphabet(listOf("G", "H", "I")) ) println("**** Map ****") val mapLists = listofAlphabets.map { it.items } println(mapLists) println("**** FlatMap ****") val flatMapList = listofAlphabets.flatMap {it.items} println(flatMapList) }
*** Output ***
**** Map ****
[[A, B, C], [D, E, F], [G, H, I]]
**** FlatMap ****
[A, B, C, D, E, F, G, H, I]
Map : Returns a list containing the results of applying the given [transform] function
to each element in the original collection
FlatMap : Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original collection.