Returns the given iterator itself. This allows to use an instance of iterator in a for
loop.
import java.util.*
fun main(args: Array<String>) {
//sampleStart
val mutableList = mutableListOf(1, 2, 3)
val mutableIterator = mutableList.iterator()
// iterator() extension is called here
for (e in mutableIterator) {
if (e % 2 == 0) {
// we can remove items from the iterator without getting ConcurrentModificationException
// because it's the same iterator that is iterated with for loop
mutableIterator.remove()
}
println("The element is $e")
}
//sampleEnd
}
@JvmName("mutableIterator") operator fun <K, V> MutableMap<K, V>.iterator(): MutableIterator<MutableEntry<K, V>>
Returns a MutableIterator over the mutable entries in the MutableMap.
© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/iterator.html