setOfNotNull

Platform and version requirements: JVM (1.4), JS (1.4), 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
}
Platform and version requirements: JVM (1.4), JS (1.4), 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
}

© 2010–2021 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/set-of-not-null.html