LongIterator

abstract class LongIterator : Iterator<Long> (source)

An iterator over a sequence of values of type Long.

This is a substitute for Iterator<Long> that provides a specialized version of next(): T method: nextLong(): Long and has a special handling by the compiler to avoid platform-specific boxing conversions as a performance optimization.

In the following example:

class LongContainer(private val data: LongArray) {

// LongIterator instead of Iterator<Long> in the signature
operator fun iterator(): LongIterator = object : LongIterator() {
private var idx = 0

override fun nextLong(): Long {
if (!hasNext()) throw NoSuchElementException()
return data[idx++]
}

override fun hasNext(): Boolean = idx < data.size
}
}

for (element in LongContainer(longArrayOf(1, 2, 3))) {
... handle element ...
}

No boxing conversion is performed during the for-loop iteration. Note that the iterator itself will still be allocated.

Since Kotlin

1.0

Constructors

Link copied to clipboard
constructor()

Functions

Link copied to clipboard

Creates a sequence that returns all elements from this iterator. The sequence is constrained to be iterated only once.

Since Kotlin 1.0
Link copied to clipboard
inline fun <T> Iterator<T>.forEach(operation: (T) -> Unit)

Performs the given operation on each element of this Iterator.

Since Kotlin 1.0
Link copied to clipboard
expect abstract operator fun hasNext(): Boolean

Returns true if the iteration has more elements.

Since Kotlin 1.0
Link copied to clipboard
inline operator fun <T> Iterator<T>.iterator(): Iterator<T>

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

Since Kotlin 1.0
Link copied to clipboard
operator override fun next(): Long

Returns the next element in the iteration.

Since Kotlin 1.0
Link copied to clipboard
abstract fun nextLong(): Long

Returns the next element in the iteration without boxing conversion.

Since Kotlin 1.0
Link copied to clipboard

Returns an Iterator that wraps each element produced by the original iterator into an IndexedValue containing the index of that element and the element itself.

Since Kotlin 1.0