Using Sequence Operators: map and filter in Kotlin
📖 Scenario: You are working on a simple Kotlin program that processes a list of ages to find which people are adults and then convert their ages to a string format for display.
🎯 Goal: Build a Kotlin program that uses sequence operators map and filter to select adults (age 18 or older) from a list of ages and then convert those ages to strings.
📋 What You'll Learn
Create a list of ages with exact values
Create a sequence from the list
Use filter to keep only ages 18 or older
Use map to convert the filtered ages to strings
Print the resulting list of strings
💡 Why This Matters
🌍 Real World
Filtering and transforming data is common in apps that process user information, like showing only adult users or formatting data for display.
💼 Career
Understanding sequence operators like <code>map</code> and <code>filter</code> is essential for Kotlin developers working on data processing, Android apps, or backend services.
Progress0 / 4 steps
1
Create the list of ages
Create a list called ages with these exact integer values: 15, 22, 17, 30, 14, 19.
Kotlin
Hint
Use listOf to create the list with the exact numbers.
2
Create a sequence from the list
Create a variable called ageSequence by calling asSequence() on the ages list.
Kotlin
Hint
Use asSequence() on the ages list to create a sequence.
3
Filter adults and map to strings
Create a variable called adultAgesStrings by applying filter on ageSequence to keep ages 18 or older, then apply map to convert each age to a string, and finally convert the result to a list using toList().
Kotlin
Hint
Use filter { it >= 18 } to keep adults, then map { it.toString() } to convert to strings, and toList() to get a list.
4
Print the resulting list
Write a println statement to print the adultAgesStrings list.
Kotlin
Hint
Use println(adultAgesStrings) to display the list.
Practice
(1/5)
1. What does the map operator do in a Kotlin sequence?
easy
A. It removes items that do not match a condition.
B. It transforms each item in the sequence to a new value.
C. It sorts the items in the sequence.
D. It counts the number of items in the sequence.
Solution
Step 1: Understand the purpose of map
The map operator applies a function to each item, changing it to a new value.
Step 2: Compare with other operators
filter removes items, sort orders items, and count counts items, so these are different.
Final Answer:
It transforms each item in the sequence to a new value. -> Option B
Hint: filter uses braces with condition inside [OK]
Common Mistakes:
Using map instead of filter
Missing curly braces in lambda
Wrong condition inside lambda
3. What is the output of this Kotlin code?
val numbers = sequenceOf(1, 2, 3, 4)
val result = numbers.filter { it % 2 == 0 }.map { it * 10 }.toList()
println(result)
medium
A. [10, 20]
B. [2, 4]
C. [20, 40]
D. [1, 3]
Solution
Step 1: Apply filter to keep even numbers
Filter keeps numbers where it % 2 == 0, so 2 and 4 remain.
Step 2: Apply map to multiply each by 10
Mapping multiplies each by 10: 2 * 10 = 20, 4 * 10 = 40.
Final Answer:
[20, 40] -> Option C
Quick Check:
filter evens then map *10 = [20, 40] [OK]
Hint: Filter first, then map transforms filtered items [OK]
Common Mistakes:
Mapping before filtering
Confusing filter condition
Forgetting to convert sequence to list
4. Find the error in this Kotlin code snippet:
val seq = sequenceOf(1, 2, 3, 4)
val filtered = seq.filter(it % 2 == 0)
println(filtered.toList())
medium
A. Missing curly braces around lambda in filter
B. Using map instead of filter
C. Sequence cannot be converted to list
D. Filter condition should be it % 2 != 0
Solution
Step 1: Check filter lambda syntax
Filter requires a lambda with curly braces: { it % 2 == 0 }. The code misses braces.
Step 2: Validate other options
Map is not used here; sequence can convert to list; condition can be any boolean expression.
Final Answer:
Missing curly braces around lambda in filter -> Option A
Quick Check:
filter needs braces for lambda [OK]
Hint: Always use braces {} for lambdas in filter [OK]
Common Mistakes:
Omitting braces in lambda
Confusing filter and map
Wrong filter condition assumed as error
5. Given a sequence of strings seq = sequenceOf("apple", "banana", "cherry", "date"), which Kotlin code correctly filters strings containing letter 'a' and converts them to uppercase?
hard
A. seq.filter { it.contains('a') }.map { it.lowercase() }.toList()
B. seq.map { it.uppercase() }.filter { it.contains('a') }.toList()
C. seq.map { it.lowercase() }.filter { it.contains('a') }.toList()
D. seq.filter { it.contains('a') }.map { it.uppercase() }.toList()
Solution
Step 1: Filter strings containing 'a'
Use filter { it.contains('a') } to keep only strings with 'a'. This keeps "apple", "banana", "date".
Step 2: Convert filtered strings to uppercase
Use map { it.uppercase() } to change these strings to uppercase.
Final Answer:
seq.filter { it.contains('a') }.map { it.uppercase() }.toList() -> Option D