filter, map : ๋ฆฌ์คํธ๋ฅผ ์ ๋ ฅ๋ฐ์ ๋ฆฌ์คํธ๋ฅผ ๋ฐํํจ
val numbers = listOf(1, 2, 3, 4, 5)
val squaredNumbers = numbers.map { it * it }
println(squaredNumbers)
[1, 4, 9, 16, 25]
val strings = listOf("apple", "banana", "cherry")
val lengths = strings.map { it.length }
println(lengths)
[5, 6, 6]
data class Person(val name: String, val age: Int)
val people = listOf(Person("Alice", 25), Person("Bob", 30), Person("Charlie", 22))
val name = people.map { it.name }
println(people)
println(name)
[Person(name=Alice, age=25), Person(name=Bob, age=30), Person(name=Charlie, age=22)]
[Alice, Bob, Charlie]
val intNumbers = listOf(1, 2, 3, 4, 5)
val stringNumbers = intNumbers.map { it.toString() }
println(stringNumbers)
[1, 2, 3, 4, 5]
val cities = listOf("Seoul", "Tokyo", "New york")
//1
cities.map{
println(it.toUpperCase())
}
//2
cities.map{city->city.toUpperCase()}
.forEach{println(it)}
SEOUL
TOKYO
NEW YORK
SEOUL
TOKYO
NEW YORK
val cities = listOf("Seoul", "Tokyo", "New york")
//1
cities.map{
println(it.length)
}
//2
cities.map{ city -> city.length }
.forEach{ println("LENGTH:$it")}
cities.mapNotNull { city -> if(city.length < 6) city else null }
.forEach{ println(it) }
}
5
5
8
LENGTH:5
LENGTH:5
LENGTH:8
Seoul
Tokyo