Bird
0
0

How can you combine destructuring with a filter to create a list of values from a map where keys start with 'A'?

hard📝 Application Q9 of 15
Kotlin - Collections Fundamentals
How can you combine destructuring with a filter to create a list of values from a map where keys start with 'A'?
Amap.filterKeys { it.startsWith("A") }.values.toList()
Bmap.map { (k, v) -> if (k.startsWith("A")) v else null }
Cmap.filter { (k, _) -> k.startsWith("A") }.map { (_, v) -> v }
Dmap.filterValues { it.startsWith("A") }.keys.toList()
Step-by-Step Solution
Solution:
  1. Step 1: Use filter with destructuring to check keys

    Filter entries where key starts with 'A' using (k, _) destructuring.
  2. Step 2: Map filtered entries to values

    Extract values from filtered entries using (_, v) destructuring.
  3. Final Answer:

    map.filter { (k, _) -> k.startsWith("A") }.map { (_, v) -> v } -> Option C
  4. Quick Check:

    Filter keys with destructuring, then map values [OK]
Quick Trick: Filter with (k, _) then map with (_, v) [OK]
Common Mistakes:
MISTAKES
  • Filtering values instead of keys
  • Using map with if-else returning nulls
  • Confusing keys and values in filter/map

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes