본문 바로가기
코틀린/[Aggregation] 집계 작업

[Kotlin][Collection] count

by jinwo_o 2024. 8. 14.

주어진 조건자와 일치하는 요소의 수를 반환합니다.

public fun <T> Iterable<T>.count(): Int {
    if (this is Collection) return size
    var count = 0
    for (element in this) checkCountOverflow(++count)
    return count
}


print(listOf(1, 2, 3, 4).count { it > 1 }) // 3

 

count - Kotlin Programming Language

 

kotlinlang.org

'코틀린 > [Aggregation] 집계 작업' 카테고리의 다른 글

[Kotlin][Collection] groupBy  (0) 2024.09.04
[Kotlin][Collection] average  (0) 2024.08.13
[Kotlin][Collection] sum / sumOf  (0) 2024.08.13