count

받은 조건보다 큰 것들의 개수를 리턴한다

public inline fun <T> Iterable<T>.count(predicate: (T) -> Boolean): Int {
    var count = 0
    for (element in this) if (predicate(element)) count++
    return count
}

val list = listOf(1, 2, 3, 4, 5)

println(list.count{ it > 3 }) // 2
println(list.count{ it > 0 }) // 5
println(list.count{ it > 5 }) // 0

Last updated