setOf

fun <T> setOf(vararg elements: T): Set<T>(source)

Returns a new read-only set with the given elements. Elements of the set are iterated in the order they were specified. The returned set is serializable (JVM).

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val set1 = setOf(1, 2, 3)
val set2 = setOf(3, 2, 1)

// setOf preserves the iteration order of elements
println(set1) // [1, 2, 3]
println(set2) // [3, 2, 1]

// but the sets with the same elements are equal no matter of order
println("set1 == set2 is ${set1 == set2}") // true 
   //sampleEnd
}

inline fun <T> setOf(): Set<T>(source)

Returns an empty read-only set. The returned set is serializable (JVM).

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
               val set = setOf<String>()
            println("set.isEmpty() is ${set.isEmpty()}") // true

            // another way to create an empty set,
            // type parameter is inferred from the expected type
            val other: Set<Int> = emptySet()

            // "Empty sets are equal"
println("set == other is ${set == other}") // true
            println(set) // [] 
   //sampleEnd
}

expect fun <T> setOf(element: T): Set<T>(source)

Returns a new read-only set containing only the specified object element.

The returned set is serializable (JVM).

Since Kotlin

1.9

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val set = setOf('a')
println(set) // [a]
println(set.size) // 1 
   //sampleEnd
}
actual fun <T> setOf(element: T): Set<T>(source)

Returns a new read-only set containing only the specified object element.

Since Kotlin

1.1

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val set = setOf('a')
println(set) // [a]
println(set.size) // 1 
   //sampleEnd
}
actual fun <T> setOf(element: T): Set<T>(source)

Returns a new read-only set containing only the specified object element.

The returned set is serializable.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val set = setOf('a')
println(set) // [a]
println(set.size) // 1 
   //sampleEnd
}
actual fun <T> setOf(element: T): Set<T>(source)

Returns a new read-only set containing only the specified object element.

Since Kotlin

1.3

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val set = setOf('a')
println(set) // [a]
println(set.size) // 1 
   //sampleEnd
}
actual fun <T> setOf(element: T): Set<T>(source)

Returns a new read-only set containing only the specified object element.

Since Kotlin

1.8

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val set = setOf('a')
println(set) // [a]
println(set.size) // 1 
   //sampleEnd
}
actual fun <T> setOf(element: T): Set<T>(source)

Returns a new read-only set containing only the specified object element.

Since Kotlin

1.8

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val set = setOf('a')
println(set) // [a]
println(set.size) // 1 
   //sampleEnd
}