map
Returns a list containing the results of applying the given transform function to each element in the original array.
Samples
import samples.*
import kotlin.test.*
fun main() {
//sampleStart
val numbers = listOf(1, 2, 3)
assertPrints(numbers.map { it * it }, "[1, 4, 9]")
//sampleEnd
}
Returns a list containing the results of applying the given transform function to each element in the original collection.
Samples
import samples.*
import kotlin.test.*
fun main() {
//sampleStart
val numbers = listOf(1, 2, 3)
assertPrints(numbers.map { it * it }, "[1, 4, 9]")
//sampleEnd
}
inline fun <K, V, R> Map<out K, V>.map(transform: (Map.Entry<K, V>) -> R): List<R>
Content copied to clipboard
Returns a list containing the results of applying the given transform function to each entry in the original map.
Samples
import samples.*
import kotlin.test.*
import java.util.*
fun main() {
//sampleStart
val peopleToAge = mapOf("Alice" to 20, "Bob" to 21)
assertPrints(
peopleToAge.map { (name, age) -> "$name is $age years old" },
"[Alice is 20 years old, Bob is 21 years old]"
)
assertPrints(peopleToAge.map { it.value }, "[20, 21]")
//sampleEnd
}
Returns a list containing the results of applying the given transform function to each element in the original array.
Since Kotlin
1.3
Samples
import samples.*
import kotlin.test.*
fun main() {
//sampleStart
val numbers = listOf(1, 2, 3)
assertPrints(numbers.map { it * it }, "[1, 4, 9]")
//sampleEnd
}