Kotlin setOf() Example

The setOf() method creates a new read-only set in Kotlin. A set cannot contain duplicate elements.

Kotlin setOf() Example

The example creates a new set with setOf(). The size of the set is determined with the size attribute.
package net.sourcecodeexamples.kotlin

fun main() {
 // Set contains unique elements
 val theSet = setOf("one", "two", "three", "one")
 println("The set contains ${theSet.size} elements.")
 val theMutableOf = mutableSetOf("one", "two", "three")
[one, two, three]
The set contains 3 elements.
[one, two, three]

Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course