Kotlin Set first and last Element Example

This example shows how to get first and the last elements of the Set in Kotlin.

Kotlin Set first and last elements example

The example creates a list of fruits. We get the first and the last elements of the list.
package net.sourcecodeexamples.kotlin

fun main() {

    val fruits = setOf("banana", "mango", "apple", "orange")

    val w1 = fruits.first()
    println(w1)

    val w2 = fruits.last()
    println(w2)

    val w3 = fruits.findLast { w -> w.startsWith('a') }
    println(w3)

    val w4 = fruits.first { w -> w.startsWith('o') }
    println(w4)
}
Output:
banana
orange
apple
orange




Comments