setOfNotNull

Common
JVM
JS
Native
1.4
fun <T : Any> setOfNotNull(element: T?): Set<T>

Returns a new read-only set either with single given element, if it is not null, or empty set if the element is null. The returned set is serializable (JVM).

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val empty = setOfNotNull<Any>(null)
println(empty) // []

val singleton = setOfNotNull(42)
println(singleton) // [42]

val set = setOfNotNull(1, null, 2, null, 3)
println(set) // [1, 2, 3]
//sampleEnd
}
Common
JVM
JS
Native
1.4
fun <T : Any> setOfNotNull(vararg elements: T?): Set<T>

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

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val empty = setOfNotNull<Any>(null)
println(empty) // []

val singleton = setOfNotNull(42)
println(singleton) // [42]

val set = setOfNotNull(1, null, 2, null, 3)
println(set) // [1, 2, 3]
//sampleEnd
}