Server Side Kotlin-16

Server Side Kotlin-16

·

1 min read

Operation which returns a single value depends upon collection content is known as aggregate function, present in Kotlin collections.

minOrNull() and maxOrNull() return the smallest and the largest element respectively. However On empty collections, they return null.

average() returns the average value of all elements in the collection of numbers.

sum() returns the sum of all elements in the collection of numbers.

count() returns the number of all elements in a collection.

 val numbers = listOf(66, 642, 610, 64)

    println("Count: ${numbers.count()}")
    println("Max: ${numbers.maxOrNull()}")
    println("Min: ${numbers.minOrNull()}")
    println("Average: ${numbers.average()}")
    println("Sum: ${numbers.sum()}")

Output

Count: 4

Max: 642

Min: 64

Average: 345.5

Sum: 1382