0Pricing
Kotlin Academy · Lesson

Aggregation

sum, count, fold, reduce.

Aggregating Collections

Aggregation reduces a collection to a single value. Kotlin provides ready-made functions like sum, count, average, max, and general-purpose fold and reduce.

sum and average

sum adds up numeric elements, and average returns their mean as a Double.

fun main() {
    val numbers = listOf(2, 4, 6, 8)
    println("Sum: ${numbers.sum()}")
    println("Average: ${numbers.average()}")
}

All lessons in this course

  1. Filtering and Mapping
  2. Grouping and Partitioning
  3. Aggregation
  4. Association and Flattening
← Back to Kotlin Academy