Skip to content

Commit f10ea03

Browse files
committed
Provide extension to get mutable iterator from the mutable map
#KT-18992 Fixed
1 parent e0dc7a2 commit f10ea03

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

libraries/stdlib/src/kotlin/collections/Maps.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,7 @@ public inline operator fun <K, V> Map<out K, V>.iterator(): Iterator<Map.Entry<K
298298
* Returns a [MutableIterator] over the mutable entries in the [MutableMap].
299299
*
300300
*/
301-
@JvmVersion
302-
@JvmName("mutableIterator")
301+
@kotlin.jvm.JvmName("mutableIterator")
303302
@kotlin.internal.InlineOnly
304303
public inline operator fun <K, V> MutableMap<K, V>.iterator(): MutableIterator<MutableMap.MutableEntry<K, V>> = entries.iterator()
305304

libraries/stdlib/test/collections/MapTest.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ class MapTest {
9797
assertEquals("beverage,beer,location,Mells,name,James", list.joinToString(","))
9898
}
9999

100+
@Test fun iterateAndMutate() {
101+
val map = mutableMapOf("beverage" to "beer", "location" to "Mells", "name" to "James")
102+
val it = map.iterator()
103+
for (e in it) {
104+
when (e.key) {
105+
"beverage" -> e.setValue("juice")
106+
"location" -> it.remove()
107+
}
108+
}
109+
assertEquals(mapOf("beverage" to "juice", "name" to "James"), map)
110+
}
111+
100112

101113
@Test
102114
fun onEach() {

0 commit comments

Comments
 (0)