Add setOfNotNull function #KT-35851

This commit is contained in:
Abduqodiri Qurbonzoda
2020-02-13 14:46:14 +03:00
parent 5a4ce2aa4c
commit ce8e511b79
4 changed files with 45 additions and 0 deletions
@@ -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.