본문 바로가기
프로그래밍/Kotlin

Kotlin] List 기능 설명(vector와 array의 차이점)

by Hwan2 2020. 7. 18.
728x90
반응형

1. List

Kotlin에선 Collection 형태의 3가지 자료구조를 제공합니다.
List, Set, Map 이렇게 말이죠.

Kotlin의 List는 여러가지 제공되는 함수로 최적화를 진행하게 됩니다.

하여 List의 함수 사용법, java Vector와의 차이점을 한번 알아보고자 합니다.

List에는 읽기전용인 listOf,
읽기, 쓰기 전용인 mutableListOf가 있습니다.

여기선 읽기전용인 listOf에 대해 설명하고자 합니다.


2. List vs Array vs Vector

import java.util.*
import kotlin.system.measureNanoTime

fun loop(i: Int){
for(i in 0..i){}
}

fun main() {
val list: List<Int> = (1..100000).toList()
val arr = IntArray(100000) { i -> i}
val v = Vector<Int>()
for(i in 1..100000) v.add(i)

println("list : ForLoop Time: " + measureNanoTime {
for (i in list) { loop(i) }
})

println("list : ForEach Time: " + measureNanoTime {
list.forEach { i -> loop(i) }
})

println("Array : ForLoop Time: " + measureNanoTime {
for (i in arr) { loop(i) }
})

println("Array : ForEach Time: " + measureNanoTime {
arr.forEach { i-> loop(i) }
})

println("Vector : ForLoop Time: " + measureNanoTime {
for (i in v) { loop(i) }
})

println("Vector : ForEach Time: " + measureNanoTime {
v.forEach { i -> loop(i) }
})
}




주목해야할 점은 List의 for문과 forEach문의 속도 차이입니다.

Collection의 forEach문에선 많은 최적화가 일어나게 됩니다. 

Kotlin은 Collection클래스에 대해 inline화를 제공하기 때문이죠.


하여 Kotlin이 Vector를 안만들고 List 하나만을 만든 이유가 여기있다고 생각합니다.

자료구조를 최소화 하면서 퍼포먼스는 많이 따라잡는.... 2마리 토끼를 잡은 샘이죠.



3. List 함수 설명

1) List init

fun main() {
val list1: List<Int> = (1..100000).toList()
val list2: List<Int> = listOf(1, 2, 3, 4, 5)

var list3: List<Int> = listOf()
val arr = arrayOf(10, 20, 30, 40)
list3 = arr.toList()

val list4 = listOf(1, 2, null, 3) //output : 1, 2, null, 3
val list5: List<Int> = listOfNotNull(null, 1, null, 2, null, 3)//output : 1, 2, 3
}

listOfNotNull은 null이 입력되있으면 이를 생략합니다.

List<Int>는 생략해도 됩니다.(자료형 생략 가능)


2) List size

fun main() {
val list: List<Int> = listOf(1, 2, 3, 4, 5)
println(list.size) //output : 5
}


3) List contains, containsAll

fun main() {
val list: List<Int> = listOf(1, 2, 3, 4, 5)
println(list.contains(5)) //true
println(list.contains(10)) //false

val com_list1: List<Int> = listOf(1, 2, 3)
val com_list2: List<Int> = listOf(10, 2, 30, 4, 5)

println(list.containsAll(com_list1)) //true
println(list.containsAll(com_list2)) //false
}

contains는 boolean을 반환합니다.

또한 containsAll은 입력받은 Collection(List)전체를 비교합니다.


4) List get

fun main() {
val list: List<Int> = listOf(1, 2, 3, 4, 5)
println(list.get(0)) //output : 1
println(list.get(4)) //output : 5
println(list.get(10)) //error (ArrayIndexOutOfBoundsException)
}

get은 List의 index위치에 있는 요소(element)를 반환합니다.


5) List indexOf, lastIndexOf

fun main() {
val list: List<Int> = listOf(10, 20, 11, 23, 55)
println(list.indexOf(0)) //output : -1
println(list.indexOf(55)) //output : 4
println(list.indexOf(11)) //output : 2
println(list.lastIndexOf(0)) //output : -1
println(list.lastIndexOf(23)) //output : 3
}

indexOf와 lastIndexOf는 List의 요소(element)값에 대한 index값을 반환합니다.

해당 요소가 없을 시 -1을 반환합니다.


indexOf는 앞에서부터 값을 찾는다면,

lastIndexOf는 마지막에서부터 값을 찾습니다.



6) List isEmpty

fun main() {
val list: List<Int> = listOf(10, 20, 11, 23, 55)
val empty_list: List<Int> = listOf()

println(list.isEmpty()) //false
println(empty_list.isEmpty()) //true
}

List가 비어있는지 확인하는 함수입니다.



7) List iterator, listIterator

fun main() {
val list: List<Int> = listOf(10, 20, 11, 23, 55)
val it1 = list.iterator()
val it2 = list.listIterator()

while(it1.hasNext() == true){
println(it1.next()) //output : 10, 20, 11, 23, 55
}

while(it2.hasNext() == true){
println(it2.next()) //output : 10, 20, 11, 23, 55
}
}

List의 iterator반환



8) List lastIndexOf

fun main() {
val list: List<Int> = listOf(10, 20, 11, 23, 55)

val sub_list1 = list.subList(2, 2)
val sub_list2 = list.subList(2, 3)
val sub_list3 = list.subList(2, 4)
val sub_list4 = list.subList(2, 5)

println(sub_list1) //output : null
println(sub_list2) //output : 11
println(sub_list3) //output : 11, 23
println(sub_list4) //output : 11, 23, 55
}

subList 즉, 원하는 범위를 잘라서 해당 Collection을 반환합니다.



9) List indices

fun main() {
val list: List<Int> = listOf(10, 20, 11, 23, 55)

for(i in list.indices)
println(list[i]) //output : 10, 20, 11, 23, 55

println(list.indices) //output : 0..4
}

indices는 범위 값을 반환합니다.

현재 list에는 0 ~ 4까지 있으므로 0..4를 반환하게 됩니다.


10) List lastIndex

fun main() {
val list: List<Int> = listOf(10, 20, 11, 23, 55)

println(list.lastIndex) //output : 4
}

해당 List의 마지막 index번호를 반환합니다.





Kotlin List reference : https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/



반응형

댓글


스킨편집 -> html 편집에서