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...