Provide extension to get mutable iterator from the mutable map

#KT-18992 Fixed
This commit is contained in:
Ilya Gorbunov
2017-07-21 23:44:30 +03:00
parent e0dc7a27a0
commit f10ea03173
2 changed files with 13 additions and 2 deletions
@@ -298,8 +298,7 @@ public inline operator fun <K, V> Map<out K, V>.iterator(): Iterator<Map.Entry<K
* Returns a [MutableIterator] over the mutable entries in the [MutableMap].
*
*/
@JvmVersion
@JvmName("mutableIterator")
@kotlin.jvm.JvmName("mutableIterator")
@kotlin.internal.InlineOnly
public inline operator fun <K, V> MutableMap<K, V>.iterator(): MutableIterator<MutableMap.MutableEntry<K, V>> = entries.iterator()
@@ -97,6 +97,18 @@ class MapTest {
assertEquals("beverage,beer,location,Mells,name,James", list.joinToString(","))
}
@Test fun iterateAndMutate() {
val map = mutableMapOf("beverage" to "beer", "location" to "Mells", "name" to "James")
val it = map.iterator()
for (e in it) {
when (e.key) {
"beverage" -> e.setValue("juice")
"location" -> it.remove()
}
}
assertEquals(mapOf("beverage" to "juice", "name" to "James"), map)
}
@Test
fun onEach() {