Hanbit the Developer

Kotlin Documentation | Constructing collections 본문

Mobile/Kotlin

Kotlin Documentation | Constructing collections

hanbikan 2023. 5. 24. 13:44

Kotlin Documentation 시리즈에 대해

Category: Standard library - Collections

문서 링크: https://kotlinlang.org/docs/constructing-collections.html


Create with collection builder functions

val map = buildMap { // this is MutableMap<String, Int>, types of key and value are inferred from the `put()` calls below
    put("a", 1)
    put("b", 0)
    put("c", 4)
}

println(map) // {a=1, b=0, c=4}

Initializer functions for lists

val doubled = List(3, { it * 2 })  // or MutableList if you want to change its content later
println(doubled)

→ [0, 2, 4]

Copy

toList(), toMutableList(), toSet()를 통해 컬렉션을 deepcopy 할 수 있다.