Skip to main content

Command Palette

Search for a command to run...

Server Side Kotlin-16

Updated
1 min read
Server Side Kotlin-16

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

More from this blog

DevNation

65 posts

Blog on System Programming Languages

kotlin kotlin beginner kotlin multiplateform