Kotlin - 컬렉션 생성

CODEDRAGON Development/Algorithm, DataStructure

반응형


 

 

컬렉션 생성(Kotlin)

kotlin.collections 패키지 안에 JCF(Java Collection Framework) 클래스와 매칭되는 클래스와 코틀린에 추가된 클래스들이 존재합니다.

type

함수명

수정가능 여부

반환 타입(실제 타입

List

listOf()

X (immutable)

kotlin.collections.List

List

mutableListOf()

O (mutable)

kotlin.collections.MutableList

List

arrayListOf()

O

kotlin.collections.ArrayList

(java.utlil.ArrayList)

 

 

 

 

Set

setOf()

X

kotlin.collections.Set

Set

mutableSetOf()

O

kotlin.collections.MutableSet

Set

hashSetOf()

O

kotlin.collections.HashSet

(java.utlil.HashSet)

Set

linkedSetOf()

O

kotlin.collections.LinkedHashSet

(java.utlil.LinkedHashSet)

Set

sortedSetOf()

O

kotlin.collections.TreeSet

(java.util.TreeSet)

 

 

 

 

Map

mapOf()

X

kotlin.collections.Map

Map

mutableMapOf()

O

kotlin.collections.MutableMap

Map

hashMapOf()

O

kotlin.collections.HashMap

(java.utlil.HashMap)

Map

linkedMapOf()

O

kotlin.collections.LinkedHashMap

(java.util.LinkedHashMap)

Map

sortedMapOf()

O

kotlin.collections.SortedMap

(java.util.SoredMap)

 

intellij IDE에서 임포트한 클래스의 정의 부분을 보면 Kotlin Java Collections typealias 매핑된 것을 확인할 있습니다.

 

C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.2.4\plugins\Kotlin\kotlinc\lib\kotlin-stdlib-sources.jar!\kotlin\collections\TypeAliases.kt


 


 

 

 

TypeAliases.kt

/*

 * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license

 * that can be found in the license/LICENSE.txt file.

 */

 

@file:Suppress("ACTUAL_WITHOUT_EXPECT") // for building kotlin-stdlib-minimal-for-test

 

package kotlin.collections

 

@SinceKotlin("1.1") public actual typealias RandomAccess = java.util.RandomAccess

 

 

@SinceKotlin("1.1") public actual typealias ArrayList<E> = java.util.ArrayList<E>

@SinceKotlin("1.1") public actual typealias LinkedHashMap<K, V> = java.util.LinkedHashMap<K, V>

@SinceKotlin("1.1") public actual typealias HashMap<K, V> = java.util.HashMap<K, V>

@SinceKotlin("1.1") public actual typealias LinkedHashSet<E> = java.util.LinkedHashSet<E>

@SinceKotlin("1.1") public actual typealias HashSet<E> = java.util.HashSet<E>