Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public expect abstract class AbstractMutableMap<K, V> : MutableMap<K, V> {
/**
* Associates the specified [value] with the specified [key] in the map.
*
* If the map already contains a mapping for [key], the key instance stored in the map is retained.
*
* This method is redeclared as abstract, because it's not implemented in the base class,
* so it must be always overridden in the concrete mutable collection implementation.
*
Expand Down
21 changes: 21 additions & 0 deletions libraries/stdlib/js/builtins/Collections.kt
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,12 @@ public actual interface MutableSet<E> : Set<E>, MutableCollection<E> {
/**
* Adds the specified element to the set.
*
* If the set already contains [element], the element instance stored in the set is retained.
*
* @return `true` if the element has been added, `false` if the element is already contained in the set.
*
* @sample samples.collections.Collections.Sets.add
* @sample samples.collections.Collections.Sets.addKeepsStoredElement
*/
@JsExport.Ignore
@IgnorableReturnValue
Expand All @@ -625,6 +628,17 @@ public actual interface MutableSet<E> : Set<E>, MutableCollection<E> {

// Bulk Modification Operations

/**
* Adds all of the elements of the specified collection to the set.
*
* The effect of this call is equivalent to calling [add] for each element of [elements], so the element
* instances stored in the set are retained.
*
* @return `true` if any of the specified elements was added to the set, `false` if the set was not modified.
*
* @sample samples.collections.Collections.Sets.addAll
* @sample samples.collections.Collections.Sets.addKeepsStoredElement
*/
@JsExport.Ignore
@IgnorableReturnValue
actual override fun addAll(elements: Collection<E>): Boolean
Expand Down Expand Up @@ -853,9 +867,12 @@ public actual interface MutableMap<K, V> : Map<K, V> {
/**
* Associates the specified [value] with the specified [key] in the map.
*
* If the map already contains a mapping for [key], the key instance stored in the map is retained.
*
* @return the previous value associated with the key, or `null` if the key was not present in the map.
*
* @sample samples.collections.Maps.CoreApi.put
* @sample samples.collections.Maps.CoreApi.putKeepsStoredKey
*/
@JsExport.Ignore
@IgnorableReturnValue
Expand All @@ -876,7 +893,11 @@ public actual interface MutableMap<K, V> : Map<K, V> {
/**
* Updates this map with key/value pairs from the specified map [from].
*
* The effect of this call is equivalent to calling [put] for each entry of [from], so the key instances
* stored in this map are retained.
*
* @sample samples.collections.Maps.CoreApi.putAll
* @sample samples.collections.Maps.CoreApi.putKeepsStoredKey
*/
@JsExport.Ignore
public actual fun putAll(from: Map<out K, V>): Unit
Expand Down
21 changes: 21 additions & 0 deletions libraries/stdlib/jvm/builtins/Collections.kt
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,12 @@ public actual interface MutableSet<E> : Set<E>, MutableCollection<E> {
/**
* Adds the specified element to the set.
*
* If the set already contains [element], the element instance stored in the set is retained.
*
* @return `true` if the element has been added, `false` if the element is already contained in the set.
*
* @sample samples.collections.Collections.Sets.add
* @sample samples.collections.Collections.Sets.addKeepsStoredElement
*/
@IgnorableReturnValue
actual override fun add(element: E): Boolean
Expand All @@ -514,6 +517,17 @@ public actual interface MutableSet<E> : Set<E>, MutableCollection<E> {

// Bulk Modification Operations

/**
* Adds all of the elements of the specified collection to the set.
*
* The effect of this call is equivalent to calling [add] for each element of [elements], so the element
* instances stored in the set are retained.
*
* @return `true` if any of the specified elements was added to the set, `false` if the set was not modified.
*
* @sample samples.collections.Collections.Sets.addAll
* @sample samples.collections.Collections.Sets.addKeepsStoredElement
*/
@IgnorableReturnValue
actual override fun addAll(elements: Collection<E>): Boolean
@IgnorableReturnValue
Expand Down Expand Up @@ -696,9 +710,12 @@ public actual interface MutableMap<K, V> : Map<K, V> {
/**
* Associates the specified [value] with the specified [key] in the map.
*
* If the map already contains a mapping for [key], the key instance stored in the map is retained.
*
* @return the previous value associated with the key, or `null` if the key was not present in the map.
*
* @sample samples.collections.Maps.CoreApi.put
* @sample samples.collections.Maps.CoreApi.putKeepsStoredKey
*/
@IgnorableReturnValue
public actual fun put(key: K, value: V): V?
Expand Down Expand Up @@ -730,7 +747,11 @@ public actual interface MutableMap<K, V> : Map<K, V> {
/**
* Updates this map with key/value pairs from the specified map [from].
*
* The effect of this call is equivalent to calling [put] for each entry of [from], so the key instances
* stored in this map are retained.
*
* @sample samples.collections.Maps.CoreApi.putAll
* @sample samples.collections.Maps.CoreApi.putKeepsStoredKey
*/
public actual fun putAll(from: Map<out K, V>): Unit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public actual abstract class AbstractMutableMap<K, V> protected actual construct
/**
* Associates the specified [value] with the specified [key] in the map.
*
* If the map already contains a mapping for [key], the key instance stored in the map is retained.
*
* This method is redeclared as abstract, because it's not implemented in the base class,
* so it must be always overridden in the concrete mutable collection implementation.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public actual abstract class AbstractMutableSet<E> protected actual constructor(
/**
* Adds the specified element to the set.
*
* If the set already contains [element], the element instance stored in the set is retained.
*
* This method is redeclared as abstract, because it's not implemented in the base class,
* so it must be always overridden in the concrete mutable collection implementation.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public actual abstract class AbstractMutableMap<K, V> protected actual construct
/**
* Associates the specified [value] with the specified [key] in the map.
*
* If the map already contains a mapping for [key], the key instance stored in the map is retained.
*
* @return the previous value associated with the key, or `null` if the key was not present in the map.
*/
@IgnorableReturnValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public actual abstract class AbstractMutableSet<E> protected actual constructor(
/**
* Adds the specified element to the set.
*
* If the set already contains [element], the element instance stored in the set is retained.
*
* @return `true` if the element has been added, `false` if the element is already contained in the set.
*/
@IgnorableReturnValue
Expand Down
7 changes: 7 additions & 0 deletions libraries/stdlib/native-wasm/src/kotlin/collections/Map.kt
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,12 @@ public actual interface MutableMap<K, V> : Map<K, V> {
/**
* Associates the specified [value] with the specified [key] in the map.
*
* If the map already contains a mapping for [key], the key instance stored in the map is retained.
*
* @return the previous value associated with the key, or `null` if the key was not present in the map.
*
* @sample samples.collections.Maps.CoreApi.put
* @sample samples.collections.Maps.CoreApi.putKeepsStoredKey
*/
@IgnorableReturnValue
public actual fun put(key: K, value: V): V?
Expand All @@ -183,7 +186,11 @@ public actual interface MutableMap<K, V> : Map<K, V> {
/**
* Updates this map with key/value pairs from the specified map [from].
*
* The effect of this call is equivalent to calling [put] for each entry of [from], so the key instances
* stored in this map are retained.
*
* @sample samples.collections.Maps.CoreApi.putAll
* @sample samples.collections.Maps.CoreApi.putKeepsStoredKey
*/
public actual fun putAll(from: Map<out K, V>): Unit

Expand Down
14 changes: 14 additions & 0 deletions libraries/stdlib/native-wasm/src/kotlin/collections/Set.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@ public actual interface MutableSet<E> : Set<E>, MutableCollection<E> {
/**
* Adds the specified element to the set.
*
* If the set already contains [element], the element instance stored in the set is retained.
*
* @return `true` if the element has been added, `false` if the element is already contained in the set.
*
* @sample samples.collections.Collections.Sets.add
* @sample samples.collections.Collections.Sets.addKeepsStoredElement
*/
@IgnorableReturnValue
actual override fun add(element: E): Boolean
Expand All @@ -85,6 +88,17 @@ public actual interface MutableSet<E> : Set<E>, MutableCollection<E> {
actual override fun remove(element: E): Boolean

// Bulk Modification Operations
/**
* Adds all of the elements of the specified collection to the set.
*
* The effect of this call is equivalent to calling [add] for each element of [elements], so the element
* instances stored in the set are retained.
*
* @return `true` if any of the specified elements was added to the set, `false` if the set was not modified.
*
* @sample samples.collections.Collections.Sets.addAll
* @sample samples.collections.Collections.Sets.addKeepsStoredElement
*/
@IgnorableReturnValue
actual override fun addAll(elements: Collection<E>): Boolean

Expand Down
21 changes: 21 additions & 0 deletions libraries/stdlib/samples/test/samples/collections/collections.kt
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,27 @@ class Collections {
assertPrints(set, "[a, b, c]")
}

@Sample
fun addKeepsStoredElement() {
class User(val id: Int, val name: String) {
override fun equals(other: Any?): Boolean = other is User && id == other.id
override fun hashCode(): Int = id
override fun toString(): String = name
}

val set = mutableSetOf(User(1, "Alice"))

assertPrints(set.add(User(1, "Alicia")), "false")
assertPrints(set, "[Alice]")

assertPrints(set.addAll(listOf(User(1, "Alicia"))), "false")
assertPrints(set, "[Alice]")

set.remove(User(1, "Alice"))
set.add(User(1, "Alicia"))
assertPrints(set, "[Alicia]")
}

@Sample
fun addAll() {
val set = mutableSetOf('a', 'b', 'c')
Expand Down
21 changes: 21 additions & 0 deletions libraries/stdlib/samples/test/samples/collections/maps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,27 @@ class Maps {
assertPrints(map, "{1=_ONE_, 2=two, 3=three, 4=four}")
}

@Sample
fun putKeepsStoredKey() {
class User(val id: Int, val name: String) {
override fun equals(other: Any?): Boolean = other is User && id == other.id
override fun hashCode(): Int = id
override fun toString(): String = name
}

val map = mutableMapOf(User(1, "Alice") to "admin")

map[User(1, "Alicia")] = "guest"
assertPrints(map, "{Alice=guest}")

map.putAll(mapOf(User(1, "Alicia") to "owner"))
assertPrints(map, "{Alice=owner}")

map.remove(User(1, "Alice"))
map[User(1, "Alicia")] = "guest"
assertPrints(map, "{Alicia=guest}")
}

@Sample
fun clear() {
val map = mutableMapOf(1 to "one", 2 to "two")
Expand Down
21 changes: 21 additions & 0 deletions libraries/stdlib/src/kotlin/Collections.kt
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,12 @@ public expect interface MutableSet<E> : Set<E>, MutableCollection<E> {
/**
* Adds the specified element to the set.
*
* If the set already contains [element], the element instance stored in the set is retained.
*
* @return `true` if the element has been added, `false` if the element is already contained in the set.
*
* @sample samples.collections.Collections.Sets.add
* @sample samples.collections.Collections.Sets.addKeepsStoredElement
*/
@IgnorableReturnValue
override fun add(element: E): Boolean
Expand All @@ -504,6 +507,17 @@ public expect interface MutableSet<E> : Set<E>, MutableCollection<E> {
override fun remove(element: E): Boolean

// Bulk Modification Operations
/**
* Adds all of the elements of the specified collection to the set.
*
* The effect of this call is equivalent to calling [add] for each element of [elements], so the element
* instances stored in the set are retained.
*
* @return `true` if any of the specified elements was added to the set, `false` if the set was not modified.
*
* @sample samples.collections.Collections.Sets.addAll
* @sample samples.collections.Collections.Sets.addKeepsStoredElement
*/
@IgnorableReturnValue
override fun addAll(elements: Collection<E>): Boolean
@IgnorableReturnValue
Expand Down Expand Up @@ -674,9 +688,12 @@ public expect interface MutableMap<K, V> : Map<K, V> {
/**
* Associates the specified [value] with the specified [key] in the map.
*
* If the map already contains a mapping for [key], the key instance stored in the map is retained.

@ilya-g ilya-g Aug 21, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather say what happens with the value, it would be more useful from the user perspective.

Suggested change
* If the map already contains a mapping for [key], the key instance stored in the map is retained.
* If the map already contains a mapping for [key], the value for that key is replaced with the specified [value].

*
* @return the previous value associated with the key, or `null` if the key was not present in the map.
*
* @sample samples.collections.Maps.CoreApi.put
* @sample samples.collections.Maps.CoreApi.putKeepsStoredKey

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it deserves a separate sample or even a sample at all.

*/
@IgnorableReturnValue
public fun put(key: K, value: V): V?
Expand All @@ -695,7 +712,11 @@ public expect interface MutableMap<K, V> : Map<K, V> {
/**
* Updates this map with key/value pairs from the specified map [from].
*
* The effect of this call is equivalent to calling [put] for each entry of [from], so the key instances
* stored in this map are retained.
*
* @sample samples.collections.Maps.CoreApi.putAll
* @sample samples.collections.Maps.CoreApi.putKeepsStoredKey
*/
public fun putAll(from: Map<out K, V>): Unit

Expand Down
2 changes: 2 additions & 0 deletions libraries/stdlib/src/kotlin/collections/Maps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ public inline operator fun <@kotlin.internal.OnlyInputTypes K, V> Map<out K, V>.

/**
* Allows to use the index operator for storing values in a mutable map.
*
* Equivalent to calling [MutableMap.put] with the given [key] and [value].
*/
@kotlin.internal.InlineOnly
public inline operator fun <K, V> MutableMap<K, V>.set(key: K, value: V): Unit {
Expand Down
Loading