Add setOfNotNull function #KT-35851
This commit is contained in:
@@ -90,6 +90,25 @@ public inline fun <T> linkedSetOf(): LinkedHashSet<T> = LinkedHashSet()
|
||||
*/
|
||||
public fun <T> linkedSetOf(vararg elements: T): LinkedHashSet<T> = elements.toCollection(LinkedHashSet(mapCapacity(elements.size)))
|
||||
|
||||
/**
|
||||
* 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).
|
||||
* @sample samples.collections.Collections.Sets.setOfNotNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public fun <T : Any> setOfNotNull(element: T?): Set<T> = if (element != null) setOf(element) else emptySet()
|
||||
|
||||
/**
|
||||
* 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).
|
||||
* @sample samples.collections.Collections.Sets.setOfNotNull
|
||||
*/
|
||||
@SinceKotlin("1.4")
|
||||
public fun <T : Any> setOfNotNull(vararg elements: T?): Set<T> {
|
||||
return elements.filterNotNullTo(LinkedHashSet())
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a new read-only [Set] by populating a [MutableSet] using the given [builderAction]
|
||||
* and returning a read-only set with the same elements.
|
||||
|
||||
Reference in New Issue
Block a user