It completely depends on the object associated with Iterator . Recall that (almost) every Iterator is associated with an Iterable , which we iterate over.
Each Iterable class can define its own Iterator class, which, upon request, returns the next() element.
How does he do it when data is available instantly:
- It can retrieve each item from a file, from a database, from a library, or any other.
- It can extract every element from an object that is already in memory.
- etc.
In case 1, it would probably be useful to store everything in memory, but in case 2, where we already have everything in memory, we could (and really) query this object about the element in the next position.
source share