0
0
Kotlinprogramming~30 mins

Flow operators (map, filter, transform) in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Flow operators (map, filter, transform)
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use runBlocking and collect to get the flow values and print them.