함수 | 설명 | 예제 |
---|---|---|
map | 모든 요소를 변환하여 새로운 리스트 생성 | list.map { it * 2 } |
flatMap | 각 요소를 리스트로 변환 후, 하나의 리스트로 합침 | list.flatMap { listOf(it, it * 2) } |
distinct | 중복 제거 | list.distinct() |
chunked(n) | 리스트를 n 개씩 나눠서 리스트의 리스트로 반환 | list.chunked(2) |
함수 | 설명 | 예제 |
---|---|---|
filter | 조건을 만족하는 요소만 추출 | list.filter { it > 10 } |
filterNot | 조건을 만족하지 않는 요소만 추출 | list.filterNot { it > 10 } |
filterNotNull | null 값을 제외한 리스트 반환 | listOf(1, null, 2).filterNotNull() |
take(n) | 앞에서 n 개 가져오기 | list.take(3) |
takeLast(n) | 뒤에서 n 개 가져오기 | list.takeLast(3) |
drop(n) | 앞에서 n 개 제외 | list.drop(2) |
dropLast(n) | 뒤에서 n 개 제외 | list.dropLast(2) |
함수 | 설명 | 예제 |
---|---|---|
sum | 숫자 리스트의 합 | listOf(1, 2, 3).sum() |
sumOf | 특정 속성 값을 합산 | listOf(Product("사과", 1000)).sumOf { it.price } |
average | 평균 값 계산 | listOf(1, 2, 3).average() |
count | 특정 조건을 만족하는 개수 반환 | list.count { it % 2 == 0 } |
maxOrNull | 최대값 반환 (null 가능) | list.maxOrNull() |
minOrNull | 최소값 반환 (null 가능) | list.minOrNull() |
함수 | 설명 | 예제 |
---|---|---|
sorted | 오름차순 정렬 | list.sorted() |
sortedDescending | 내림차순 정렬 | list.sortedDescending() |
sortedBy | 특정 속성을 기준으로 정렬 | list.sortedBy { it.age } |
sortedByDescending | 특정 속성을 기준으로 내림차순 정렬 | list.sortedByDescending { it.age } |
함수 | 설명 | 예제 |
---|---|---|
any | 하나라도 조건을 만족하면 true | list.any { it > 10 } |
all | 모든 요소가 조건을 만족하면 true | list.all { it > 0 } |
none | 모든 요소가 조건을 만족하지 않으면 true | list.none { it < 0 } |
contains | 특정 값 포함 여부 확인 | list.contains(5) |
함수 | 설명 | 예제 |
---|---|---|
groupBy | 특정 기준으로 그룹화 | list.groupBy { it.length } |
partition | 조건에 따라 두 개의 리스트로 분할 | list.partition { it > 10 } |
함수 | 설명 | 예제 |
---|---|---|
reduce | 리스트 요소를 누적 계산하여 하나의 결과 반환 | list.reduce { acc, num -> acc + num } |
fold | reduce 와 동일하지만 초기값 설정 가능 | list.fold(100) { acc, num -> acc + num } |
reduceRight | reduce 와 동일하지만 오른쪽부터 연산 | list.reduceRight { num, acc -> acc - num } |
map
, flatMap
filter
, take
, drop
sum
, sumOf
, count
sortedBy
, sortedDescending
any
, all
, none
groupBy
, partition
reduce
, fold