Iterators

Iterators are particularly useful to go through a collection of elements in order. Some characteristics of Kotlin's iterators are:

  • They can't retrieve an element by index, so elements can only be accessed in order
  • They have the function hasNext(), which indicates whether there are more elements
  • Elements can be retrieved in a single direction only; there's no way to retrieve previous elements
  • They can't be reset, so they can only be iterated once

To create a suspending iterator, we use the builder buildIterator(), passing it a lambda with the body of the iterator. This will return an Iterator<T> where T will be determined by the elements that the iterator yields, unless otherwise specified:

val iterator = buildIterator {
yield(1)
}

In this case, for example, iterator will be of type Iterator<Int>. If for some reason we want to override this definition, we can define a type, and it will work as long as all the values that are yielded are compliant with the type. For example, the following will work:

val iterator : Iterator<Any> = buildIterator {
yield(1)
yield(10L)
yield("Hello")
}

And the following will result in a compile error:

val iterator : Iterator<String> = buildIterator {
yield("Hello")
yield(1)
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.0.145