Recall & Review
beginner
What does the
flatMap function do in Kotlin?It transforms each element of a collection into another collection and then flattens all those collections into a single list.
Click to reveal answer
beginner
Given a list of lists, how does
flatMap help compared to using map?map returns a list of lists, while flatMap returns a single flattened list combining all inner lists.Click to reveal answer
beginner
Example: What is the output of
listOf(listOf(1, 2), listOf(3, 4)).flatMap { it }?The output is
[1, 2, 3, 4] because flatMap flattens the nested lists into one list.Click to reveal answer
intermediate
Why is
flatMap useful when working with nested collections?It simplifies code by combining mapping and flattening steps, making it easier to work with nested data structures.
Click to reveal answer
intermediate
How would you use
flatMap to get all characters from a list of words in Kotlin?Use
words.flatMap { it.toList() } to convert each word to a list of characters and flatten them into one list.Click to reveal answer
What does
flatMap return when applied to a list of lists?✗ Incorrect
flatMap flattens nested collections into one list by combining mapping and flattening.
Which Kotlin function would you use to transform and flatten a nested collection in one step?
✗ Incorrect
flatMap is designed to transform and flatten nested collections in one operation.
What is the difference between
map and flatMap?✗ Incorrect
map transforms elements but keeps nested structure; flatMap flattens the result.
What will
listOf("hi", "ok").flatMap { it.toList() } return?✗ Incorrect
Each word is converted to a list of characters, then all characters are combined into one list.
If you want to get a list of all numbers from a list of lists of numbers, which function is best?
✗ Incorrect
flatMap combines all inner lists into one list of numbers.
Explain how
flatMap works with nested collections in Kotlin.Think about turning many small lists into one big list.
You got /3 concepts.
Describe a real-life example where
flatMap would simplify working with nested data.Imagine a list of classrooms each with a list of students.
You got /3 concepts.