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(theSet)
 println("The set contains ${theSet.size} elements.")
 val theMutableOf = mutableSetOf("one", "two", "three")
 println(theMutableOf)
}
Output:
[one, two, three]
The set contains 3 elements.
[one, two, three]


Comments