setOfNotNull

fun <T : Any> setOfNotNull(element: T?): Set<T>(source)

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).

Since Kotlin

1.4

Samples

import kotlin.test.*

fun main() { 
   //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
}

fun <T : Any> setOfNotNull(vararg elements: T?): Set<T>(source)

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).

Since Kotlin

1.4

Samples

import kotlin.test.*

fun main() { 
   //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
}