일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- list
- elementAt
- CLASS
- 생명주기
- recyclerview
- Interface
- lifecycle
- animation
- union
- Kotlin
- map
- AWS
- docker-compose
- MINUS
- ReactNative
- Filter
- mongoose
- Generic
- ConstraintLayout
- docker
- class component
- vuex
- Service
- Foreign Key
- enum
- Swift
- react native
- LiveData
- function
- collection
- Today
- Total
개발 일기
List or Set에서 데이터 검색 본문
Set에서는 보통적으로는 순서가 제공되지 않습니다.
따라서 LinkedHashSet 이나 SortedSet할 경우에는 정렬을 할수 있기 때문에 순서를 알수가 있습니다. 이런 경우에 얻어올려면 어떻게 해야 할까요?
바로 elementAt()을 사용해서 알수가 있습니다.
val numbers = linkedSetOf("one", "two", "three", "four", "five")
println(numbers.elementAt(3))
val numbersSortedSet = sortedSetOf("one", "two", "three", "four")
println(numbersSortedSet.elementAt(0)) // elements are stored in the ascending order
혹은 첫번째 마지막 데이터만 가져오고 싶은 경우에는 "first()" "last()"을 사용해서 구현할수가 있습니다.
만약에 검색시 해당 포지션을 넘어가게 되면 "IndexOutOfBoundsException"에러가 뜨게 됩니다.
이런 상황을 방지하고 싶다면 "elementAtOrNull" or "elementAtOrElse" 을 사용하실수가 있습니다.
val numbers = listOf("one", "two", "three", "four", "five")
println(numbers.elementAtOrNull(5))
println(numbers.elementAtOrElse(5) { index -> "The value for index $index is undefined"})
첫번째 마지막 데이터를 가져올때 조건을 설정하고 싶다면 아래와 같이 할수가 있습니다.
val numbers = listOf("one", "two", "three", "four", "five", "six")
println(numbers.first{ it.length > 3 })
println(numbers.last { it.startsWith("f") })
three
five
val numbers = listOf("one", "two", "three", "four", "five", "six")
println(numbers.first())
println(numbers.last())
one
six
이상황에서도 Null이 발생할 경우가 있습니다. 따라서 firstOrNull() or lastOrNull()을 사용하면
해결을 할수가 있습니다. (firstOrNull은 find() 함수로도 사용이 가능하고 lastOrNull은 findLast() 함수로도 가능합니다.)
val numbers = listOf("one", "two", "three", "four", "five", "six")
println(numbers.firstOrNull { it.length > 6 })
val numbers = listOf(1, 2, 3, 4)
println(numbers.find { it % 2 == 0 })
println(numbers.findLast { it % 2 == 0 })
다음으로 List중에 Random으로 하나를 출력하고 싶으면 다음으로 할수가 있습니다.
val numbers = listOf(1, 2, 3, 4)
println(numbers.random())
List안에서 해당 데이터가 존재여부를 알고 싶으면 contains()을 사용해서 구현할수가 있습니다.
하나가 아니라 여러개일 경우에는 containsAll()을 사용해야 합니다.
val numbers = listOf("one", "two", "three", "four", "five", "six")
println(numbers.contains("four"))
println("zero" in numbers)
println(numbers.containsAll(listOf("four", "two")))
println(numbers.containsAll(listOf("one", "zero")))
List안에서 데이터가 있는지 없는지 확인하고 싶은 경우에는 isEmpty() or isNotEmpty()을 사용하실수가 있습니다.
val numbers = listOf("one", "two", "three", "four", "five", "six")
println(numbers.isEmpty())
println(numbers.isNotEmpty())
val empty = emptyList<String>()
println(empty.isEmpty())
println(empty.isNotEmpty())
'컴퓨터 언어 > kotlin' 카테고리의 다른 글
Collection Aggregate Operations (0) | 2020.06.02 |
---|---|
Collection 순서 정렬 (0) | 2020.06.01 |
Retrieving Collection Parts (0) | 2020.05.28 |
Grouping (0) | 2020.05.27 |
plus and minus Operators (0) | 2020.05.26 |