What if you could change every item in a list with just one simple command?
Why Map transformation in Kotlin? - Purpose & Use Cases
Imagine you have a list of prices in dollars, and you want to convert each price to euros by multiplying by a conversion rate. Doing this manually means writing a loop and creating a new list yourself.
Manually looping through each item is slow to write and easy to make mistakes, like forgetting to add the converted value to the new list or messing up the calculation. It also makes your code longer and harder to read.
Map transformation lets you apply a function to every item in a list quickly and clearly. You just say what to do with each item, and it returns a new list with the results, saving time and reducing errors.
val euros = mutableListOf<Double>() for (price in prices) { euros.add(price * 0.85) }
val euros = prices.map { it * 0.85 }It makes transforming collections simple and expressive, so you can focus on what you want to do, not how to do it.
Converting a list of temperatures from Celsius to Fahrenheit in a weather app with just one line of code.
Manual loops are slow and error-prone.
Map transformation applies a function to each item easily.
Code becomes shorter, clearer, and less buggy.