Hanbit the Developer

Kotlin Documentation | Transformations 본문

Mobile/Kotlin

Kotlin Documentation | Transformations

hanbikan 2023. 5. 24. 14:06

Kotlin Documentation 시리즈에 대해

Category: Standard library - Collections

문서 링크: https://kotlinlang.org/docs/collection-transformations.html


Map

  • map, mapIndexed
  • mapNotNull, mapIndexedNotNull: map + null filter
  • mapKeys, mapValues: key 또는 value에 대해서 map을 적용한 Map을 생성한다.
val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key11" to 11)
println(numbersMap.mapKeys { it.key.uppercase() })
println(numbersMap.mapValues { it.value + it.key.length })
{KEY1=1, KEY2=2, KEY3=3, KEY11=11}
{key1=5, key2=6, key3=7, key11=16}

Zip: 두 컬렉션을 Pair으로 묶어 하나의 컬렉션으로 만든다.

zip

val colors = listOf("red", "brown", "grey")
val animals = listOf("fox", "bear", "wolf")
println(colors zip animals)

val twoAnimals = listOf("fox", "bear")
println(colors.zip(twoAnimals))
[(red, fox), (brown, bear), (grey, wolf)]
[(red, fox), (brown, bear)]

transform:

val colors = listOf("red", "brown", "grey")
val animals = listOf("fox", "bear", "wolf")

println(colors.zip(animals) { color, animal -> "The ${animal.replaceFirstChar { it.uppercase() }} is $color"})
[The Fox is red, The Bear is brown, The Wolf is grey]

unzip

val numberPairs = listOf("one" to 1, "two" to 2, "three" to 3, "four" to 4)
println(numberPairs.unzip())
([one, two, three, four], [1, 2, 3, 4])

Associate: (key = 컬렉션, value = transform)의 Map을 생성한다.

associateWith

key-value로 묶어서 Map을 반환한다. key가 겹치는 게 있을 경우, 가장 마지막 원소가 맵에 남는다.

val numbers = listOf("one", "two", "three", "four")
println(numbers.associateWith { it.length })
{one=3, two=3, three=5, four=4}

associateBy

value-key로 묶어서 Map을 반환한다.

val numbers = listOf("one", "two", "three", "four")

println(numbers.associateBy { it.first().uppercaseChar() })
println(numbers.associateBy(keySelector = { it.first().uppercaseChar() }, valueTransform = { it.length }))
{O=one, T=three, F=four}
{O=3, T=5, F=4}

associate

val names = listOf("Alice Adams", "Brian Brown", "Clara Campbell")
println(names.associate { name ->
	parseFullName(name).let {
		it.lastName to it.firstName
	}
})
{Adams=Alice, Brown=Brian, Campbell=Clara}

Flatten

flatten

val numberSets = listOf(setOf(1, 2, 3), setOf(4, 5, 6), setOf(1, 2))
println(numberSets.flatten())
[1, 2, 3, 4, 5, 6, 1, 2]

flatMap

*flat after map

data class StringContainer(val values: List<String>)

fun main() {
    val containers = listOf(
        StringContainer(listOf("one", "two", "three")),
        StringContainer(listOf("four", "five", "six")),
        StringContainer(listOf("seven", "eight"))
    )
    println(containers.flatMap { it.values })
}
[one, two, three, four, five, six, seven, eight]

String representation

joinToString, joinTo

val numbers = listOf("one", "two", "three", "four")

println(numbers)         
println(numbers.joinToString())

val listString = StringBuffer("The list of numbers: ")
numbers.joinTo(listString)
println(listString)
[one, two, three, four]
one, two, three, four
The list of numbers: one, two, three, four

separator, prefix, postfix:

val numbers = listOf("one", "two", "three", "four")    
println(numbers.joinToString(separator = " | ", prefix = "start: ", postfix = ": end"))
start: one | two | three | four: end

limit, truncated:

val numbers = (1..100).toList()
println(numbers.joinToString(limit = 10, truncated = "<...>"))
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, <...>

transform:

val numbers = listOf("one", "two", "three", "four")
println(numbers.joinToString { "Element: ${it.uppercase()}" })
Element: ONE, Element: TWO, Element: THREE, Element: FOUR

'Mobile > Kotlin' 카테고리의 다른 글

Kotlin Documentation | Group elements  (0) 2023.05.24
Kotlin Documentation | Filter  (0) 2023.05.24
Kotlin Documentation | Sequences  (0) 2023.05.24
Kotlin Documentation | Ranges and progressions  (0) 2023.05.24
Kotlin Documentation | Iterators  (0) 2023.05.24