Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- ConstraintLayout
- recyclerview
- elementAt
- animation
- Foreign Key
- ReactNative
- map
- CLASS
- function
- LiveData
- enum
- vuex
- lifecycle
- docker-compose
- Kotlin
- Swift
- Interface
- 생명주기
- react native
- MINUS
- Filter
- union
- docker
- Generic
- list
- mongoose
- Service
- class component
- AWS
- collection
Archives
- Today
- Total
개발 일기
Retrieving Collection Parts 본문
Slice
Slice을 통해서 Collection에 대해서 주어진 범위에서 자를수가 있습니다.
val numbers = listOf("one", "two", "three", "four", "five", "six")
println(numbers.slice(1..3))
println(numbers.slice(0..4 step 2))
println(numbers.slice(setOf(3, 5, 0)))
[two, three, four]
[one, three, five]
[four, six, one]
Take and drop
take() 함수를 통해서 첫번째 부터 시작해서 주어진 포지션까지 얻을수가 있습니다.
반대로 주어진 포지션부터 마지막까지 얻기 위해서는 taskLast()를 사용해야합니다.
takeLast을 했을때 만약에 List범위를 넘어갔을 경우에는 전체 List을 가져옵니다.
위와 같은 형식으로 drop() , dropLast()는 다른점은 그 해당 하는 범위를 가져오는것이 아닌 제외를 합니다.
val numbers = listOf("one", "two", "three", "four", "five", "six")
println(numbers.take(3))
println(numbers.takeLast(3))
println(numbers.drop(1))
println(numbers.dropLast(5))
그다음으로 takeWhile() , takeLastWhile() , dropWhile() , dropLastWhile()이 있습니다.
이것도 위와 같은 형식으로 진행이 되지만 다른점은 포지션이 아닌 lambda 함수내에 False 만날때까지 입니다.
val numbers = listOf("one", "two", "three", "four", "five", "six")
println(numbers.takeWhile { !it.startsWith('f') })
println(numbers.takeLastWhile { it != "three" })
println(numbers.dropWhile { it.length == 3 })
println(numbers.dropLastWhile { it.contains('i') })
[one, two, three]
[four, five, six]
[three, four, five, six]
[one, two, three, four]
Chunked
chunked() 함수를 통해서 기존에 있었던 List에서 안에 주어진 값에 List size만큼 만들어서 Return 합니다.
val numbers = (0..13).toList()
println(numbers.chunked(2))
[[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10, 11], [12, 13]]
val numbers = (0..13).toList()
println(numbers.chunked(3))
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11], [12, 13]]
val numbers = (0..13).toList()
println(numbers.chunked(3) { it.sum() })
[3, 12, 21, 30, 25]
Windowed
Windowed는 List을 step만큼 넘어가면서 마지막 포지션 까지 주어진 첫번째 인자만큼 List을 잘라서 가져옵니다.
마지막 인자 같은 경우에는 마지막에 주어진 첫번째 인자만큼 사이즈가 맞지 않는 경우에도 가져올지 여부를 선택할수가 있습니다.
(Default - step = 1, partialWindows = false)
val numbers = (1..10).toList()
println(numbers.windowed(3, step = 2, partialWindows = true))
println(numbers.windowed(3) { it.sum() })
[[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9], [9, 10]]
[6, 9, 12, 15, 18, 21, 24, 27]
ZipWithNext
collection에 두개씩 가져오면서 pair 형태로 return 합니다.
val numbers = listOf("one", "two", "three", "four", "five")
println(numbers.zipWithNext())
println(numbers.zipWithNext() { s1, s2 -> s1.length > s2.length}
[(one, two), (two, three), (three, four), (four, five)]
[false, false, true, false]
'컴퓨터 언어 > kotlin' 카테고리의 다른 글
Collection 순서 정렬 (0) | 2020.06.01 |
---|---|
List or Set에서 데이터 검색 (0) | 2020.05.29 |
Grouping (0) | 2020.05.27 |
plus and minus Operators (0) | 2020.05.26 |
Filtering (0) | 2020.05.25 |
Comments