iterator

inline operator fun <T> Iterator<T>.iterator(): Iterator<T>(source)

Returns the given iterator itself. This allows to use an instance of iterator in a for loop.

Since Kotlin

1.1

Samples

import java.util.*

fun main() { 
   //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
}

inline operator fun <K, V> Map<out K, V>.iterator(): Iterator<Map.Entry<K, V>>(source)

Returns an Iterator over the entries in the Map.

Since Kotlin

1.1

Samples

import kotlin.test.*
import java.util.*

fun main() { 
   //sampleStart 
   val map = mapOf("beverage" to 2.7, "meal" to 12.4, "dessert" to 5.8)

for ((key, value) in map) {
    println("$key - $value") // prints: beverage - 2.7
                             // prints: meal - 12.4
                             // prints: dessert - 5.8
} 
   //sampleEnd
}

Returns a MutableIterator over the mutable entries in the MutableMap.

Since Kotlin

1.1
inline operator fun <T> Iterator<T>.iterator(): Iterator<T>(source)

Returns the given iterator itself. This allows to use an instance of iterator in a for loop.

Since Kotlin

1.0

Samples

import java.util.*

fun main() { 
   //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
}

inline operator fun <K, V> Map<out K, V>.iterator(): Iterator<Map.Entry<K, V>>(source)

Returns an Iterator over the entries in the Map.

Since Kotlin

1.0

Samples

import kotlin.test.*
import java.util.*

fun main() { 
   //sampleStart 
   val map = mapOf("beverage" to 2.7, "meal" to 12.4, "dessert" to 5.8)

for ((key, value) in map) {
    println("$key - $value") // prints: beverage - 2.7
                             // prints: meal - 12.4
                             // prints: dessert - 5.8
} 
   //sampleEnd
}

@JvmName(name = "mutableIterator")
inline operator fun <K, V> MutableMap<K, V>.iterator(): MutableIterator<MutableMap.MutableEntry<K, V>>(source)

Returns a MutableIterator over the mutable entries in the MutableMap.

Since Kotlin

1.0

operator fun <T> Enumeration<T>.iterator(): Iterator<T>(source)

Creates an Iterator for an java.util.Enumeration, allowing to use it in for loops.

Since Kotlin

1.0

Samples

import java.util.*

fun main() { 
   //sampleStart 
   val vector = Vector<String>().apply {
    add("RED")
    add("GREEN")
    add("BLUE")
}

// iterator() extension is called here
for (e in vector.elements()) {
    println("The element is $e")
} 
   //sampleEnd
}