Kotlin Set Example



In this example, we have a simple Kotlin Set example.

Kotlin Set Example

The example creates a set of numbers and computes some statistics.
package net.sourcecodeexamples.kotlin

fun main() {

    val nums = setOf(1, 15, 13, 8, 1, 19, 6, 12)

    val len = nums.count()
    val max = nums.max()
    val min = nums.min()
    val sum = nums.sum()
    val avg = nums.average()

    val msg = """
               max: $max, min: $min,
               count: $len, sum: $sum,
               average: $avg
              """
    println(msg.trimIndent())

}
Output:
max: 19, min: 1,
count: 7, sum: 74,
average: 10.571428571428571

Comments