Add setOfNotNull function #KT-35851
This commit is contained in:
@@ -334,6 +334,18 @@ class Collections {
|
||||
assertPrints(set, "[1, 2, 4, 5]")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun setOfNotNull() {
|
||||
val empty = setOfNotNull<Any>(null)
|
||||
assertPrints(empty, "[]")
|
||||
|
||||
val singleton = setOfNotNull(42)
|
||||
assertPrints(singleton, "[42]")
|
||||
|
||||
val set = setOfNotNull(1, null, 2, null, 3)
|
||||
assertPrints(set, "[1, 2, 3]")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun emptyLinkedHashSet() {
|
||||
val set: LinkedHashSet<Int> = linkedSetOf<Int>()
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -71,6 +71,18 @@ class CollectionTest {
|
||||
assertEquals(listOf("value1", "value2"), l3)
|
||||
}
|
||||
|
||||
@Test fun setOfNotNull() {
|
||||
val l1: Set<Int> = setOfNotNull(null)
|
||||
assertTrue(l1.isEmpty())
|
||||
|
||||
val s: String? = "value"
|
||||
val l2: Set<String> = setOfNotNull(s)
|
||||
assertEquals(s, l2.single())
|
||||
|
||||
val l3: Set<String> = setOfNotNull("value1", null, "value2")
|
||||
assertEquals(setOf("value1", "value2"), l3)
|
||||
}
|
||||
|
||||
@Test fun filterIntoSet() {
|
||||
val data = listOf("foo", "bar")
|
||||
val foo = data.filterTo(hashSetOf<String>()) { it.startsWith("f") }
|
||||
|
||||
+2
@@ -2369,6 +2369,8 @@ public final class kotlin/collections/SetsKt {
|
||||
public static final fun plus (Ljava/util/Set;[Ljava/lang/Object;)Ljava/util/Set;
|
||||
public static final fun setOf (Ljava/lang/Object;)Ljava/util/Set;
|
||||
public static final fun setOf ([Ljava/lang/Object;)Ljava/util/Set;
|
||||
public static final fun setOfNotNull (Ljava/lang/Object;)Ljava/util/Set;
|
||||
public static final fun setOfNotNull ([Ljava/lang/Object;)Ljava/util/Set;
|
||||
public static final fun sortedSetOf (Ljava/util/Comparator;[Ljava/lang/Object;)Ljava/util/TreeSet;
|
||||
public static final fun sortedSetOf ([Ljava/lang/Object;)Ljava/util/TreeSet;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user