Going through all the elements

In some cases, you want to retrieve all the elements of an iterator at once, instead of doing it one by one. Notice that because we are talking about this topic from the point of view of a data source, our use case is more oriented towards getting one element or a group of them at a time.

You can use the functions forEach() and forEachRemaining() to go through the whole iterator. Because an iterator can only go forward, both functions will work the same:

iterator.forEach {
println(it)
}
While both functions will behave the same way, using one or the other will improve the readability of the code. For example, if you know that some of the elements may have been read already, use forEachRemaining() to make it clear to anyone reading the code that some of the elements may not be in the iterator by that point.
..................Content has been hidden....................

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