diff --git a/libraries/stdlib/samples/test/samples/collections/collections.kt b/libraries/stdlib/samples/test/samples/collections/collections.kt index 82d2ae99550..a3e4c063f76 100644 --- a/libraries/stdlib/samples/test/samples/collections/collections.kt +++ b/libraries/stdlib/samples/test/samples/collections/collections.kt @@ -311,6 +311,28 @@ class Collections { // but the sets with the same elements are equal no matter of order assertTrue(set1 == set2) } + + @Sample + fun emptyMutableSet() { + val set = mutableSetOf() + assertTrue(set.isEmpty()) + + set.add(1) + set.add(2) + set.add(1) + + assertPrints(set, "[1, 2]") + } + + @Sample + fun mutableSet() { + val set = mutableSetOf(1, 2, 3) + assertPrints(set, "[1, 2, 3]") + + set.remove(3) + set += listOf(4, 5) + assertPrints(set, "[1, 2, 4, 5]") + } } class Transformations { diff --git a/libraries/stdlib/src/kotlin/collections/Sets.kt b/libraries/stdlib/src/kotlin/collections/Sets.kt index c4036f6749b..f99d26d2181 100644 --- a/libraries/stdlib/src/kotlin/collections/Sets.kt +++ b/libraries/stdlib/src/kotlin/collections/Sets.kt @@ -52,6 +52,7 @@ public inline fun setOf(): Set = emptySet() * Returns an empty new [MutableSet]. * * The returned set preserves the element iteration order. + * @sample samples.collections.Collections.Sets.emptyMutableSet */ @SinceKotlin("1.1") @kotlin.internal.InlineOnly @@ -60,6 +61,7 @@ public inline fun mutableSetOf(): MutableSet = LinkedHashSet() /** * Returns a new [MutableSet] with the given elements. * Elements of the set are iterated in the order they were specified. + * @sample samples.collections.Collections.Sets.mutableSet */ public fun mutableSetOf(vararg elements: T): MutableSet = elements.toCollection(LinkedHashSet(mapCapacity(elements.size)))