MutableMap.iterator() returns MutableIterator.

This commit is contained in:
Ilya Gorbunov
2015-11-20 22:49:09 +03:00
parent 21f509511c
commit 5aa415c609
2 changed files with 19 additions and 0 deletions
@@ -190,6 +190,14 @@ public inline fun <K, V> MutableMap<K, V>.getOrPut(key: K, defaultValue: () -> V
*/
public operator fun <K, V> Map<K, V>.iterator(): Iterator<Map.Entry<K, V>> = entries.iterator()
/**
* Returns a [MutableIterator] over the mutable entries in the [MutableMap].
*
*/
@JvmVersion
@JvmName("mutableIterator")
public operator fun <K, V> MutableMap<K, V>.iterator(): MutableIterator<MutableMap.MutableEntry<K, V>> = entries.iterator()
/**
* Populates the given `destination` [Map] with entries having the keys of this map and the values obtained
* by applying the `transform` function to each entry in this [Map].
@@ -41,6 +41,17 @@ class MapJVMTest {
assertEquals("B", prop.getProperty("b", "fail"))
}
@test fun iterateAndRemove() {
val map = (1..5).toMap({ it }, { 'a' + it }).toLinkedMap()
val iterator = map.iterator()
while (iterator.hasNext()) {
if (iterator.next().key % 2 == 0)
iterator.remove()
}
assertEquals(listOf(1, 3, 5), map.keys.toList())
assertEquals(listOf('b', 'd', 'f'), map.values.toList())
}
@test fun getOrPutFailsOnConcurrentMap() {
val map = ConcurrentHashMap<String, Int>()