📖 Scenario: You are working with a list of numbers representing daily temperatures in Celsius. You want to process this data to find interesting information using Kotlin Flow operators.
🎯 Goal: Build a Kotlin program that uses Flow operators map, filter, and transform to process a flow of temperature data and print the results.
📋 What You'll Learn
Create a flow from a list of temperatures
Use a map operator to convert Celsius to Fahrenheit
Use a filter operator to keep only temperatures above a threshold
Use a transform operator to emit a custom message for each temperature
Collect and print the final results
💡 Why This Matters
🌍 Real World
Processing streams of sensor data or user inputs often requires filtering and transforming data in real time.
💼 Career
Understanding Kotlin Flow operators is important for Android developers and backend engineers working with reactive streams.
Progress0 / 4 steps
1
Create a flow from a list of temperatures
Create a list called temperaturesCelsius with these exact values: 10, 15, 20, 25, 30. Then create a flow called temperatureFlow from this list using asFlow().
Kotlin
Hint
Use listOf to create the list and asFlow() to convert it to a flow.
2
Add a threshold variable for filtering
Create an integer variable called thresholdFahrenheit and set it to 68. This will be used to filter temperatures at or above this value in Fahrenheit.
Kotlin
Hint
Just create a simple integer variable with the exact name and value.
3
Use map, filter, and transform operators on the flow
Create a new flow called processedFlow by applying these operators on temperatureFlow: first use map to convert Celsius to Fahrenheit using the formula F = C * 9 / 5 + 32, then use filter to keep only temperatures greater than or equal to thresholdFahrenheit, and finally use transform to emit a string message in the format "Temperature: X°F is warm" where X is the temperature in Fahrenheit.
Kotlin
Hint
Chain the operators in this order: map, then filter, then transform. Use emit inside transform to send the message.
4
Collect and print the processed flow results
Use runBlocking to collect the processedFlow and print each emitted string.
Kotlin
Hint
Use runBlocking and collect to get the flow values and print them.
Practice
(1/5)
1. Which Kotlin Flow operator is used to change each item in the flow to a new value?
easy
A. filter
B. map
C. transform
D. collect
Solution
Step 1: Understand the purpose of map
The map operator changes each item in the flow to a new value by applying a function.
Step 2: Compare with other operators
filter keeps items based on a condition, transform can emit multiple items, and collect gathers items but does not change them.
Final Answer:
map -> Option B
Quick Check:
Change each item = map [OK]
Hint: Map means change each item to a new one [OK]
Common Mistakes:
Confusing map with filter
Thinking transform only changes one item
Using collect to change items
2. Which of the following is the correct syntax to filter even numbers from a Kotlin Flow named numbers?
easy
A. numbers.collect { it % 2 == 0 }
B. numbers.map { it % 2 == 0 }
C. numbers.filter { it % 2 == 0 }
D. numbers.transform { emit(it % 2 == 0) }
Solution
Step 1: Identify the filter syntax
The filter operator takes a lambda returning a Boolean to keep items matching the condition.
Step 2: Check each option
numbers.filter { it % 2 == 0 } correctly uses filter { it % 2 == 0 } to keep even numbers. numbers.map { it % 2 == 0 } uses map which changes items to booleans. numbers.transform { emit(it % 2 == 0) } emits booleans instead of filtering. numbers.collect { it % 2 == 0 } misuses collect.
Final Answer:
numbers.filter { it % 2 == 0 } -> Option C
Quick Check:
Filter even numbers = filter { it % 2 == 0 } [OK]
Hint: Filter keeps items; map changes items [OK]
Common Mistakes:
Using map instead of filter
Misusing collect as a filter
Confusing transform syntax
3. What is the output of this Kotlin Flow code?
flowOf(1, 2, 3, 4)
.filter { it % 2 == 0 }
.map { it * 10 }
.toList()
medium
A. [20, 40]
B. [10, 20]
C. [2, 4]
D. [1, 2, 3, 4]
Solution
Step 1: Apply filter to keep even numbers
The flow starts with [1, 2, 3, 4]. After filtering even numbers, we get [2, 4].
Step 2: Apply map to multiply each by 10
Mapping [2, 4] by multiplying each by 10 results in [20, 40].
Final Answer:
[20, 40] -> Option A
Quick Check:
Filter evens then multiply by 10 = [20, 40] [OK]
Hint: Filter first, then map to change values [OK]
Common Mistakes:
Mapping before filtering
Confusing output with original list
Ignoring order of operators
4. Identify the error in this Kotlin Flow code snippet: