I know this question has been asked several times in this forum. But still I am posting as none of the answers looks concrete. Experts, could you help me understand when to use iterator. Do we use it ideally when we have a very large dataset or use it as return type of method when we have large data to return?
-
2Please turn to the [help] to learn how/what to ask here. You have serious misconceptions how this community works, and instead of telling people to "only post correct answers", you should step back and delete this question and enable yourself to do better. – GhostCat Aug 10 '18 at 07:45
2 Answers
If you look at some blogs and articles on this topic, you will often find this statement : iterators provide many methods to work on the collection while traversing it, like modifying it. You cannot do that with the 'while' or 'for' loops, these are used as read-only loops on your collection.
You may find some solutions to update a collection with for or while loops, but they may be far more difficult to implement than using an iterator.
For instance, if you try to update (ex: by removing an item) a collection while making a for loop on it, you will raise an concurrent exception in Java. In this case, you have to use an iterator.
I don't know if there is some usage on large data collections. You can read that other stackoverflow answer : why is the enhanced for loop more efficient than the normal for loop
Some references for java iterators :

- 355
- 2
- 8
Why iterators are so powerful?
Let's say we wanted the sum of any prime number smaller than a maximum value. We could first create a list of all integers up to maximum, then use our favorite algorithm to keep only the prime numbers, and then pass list to sum(). What if maximum was 1 billion? The filtered list would not only take a long time just to construct but require a needlessly large amount of memory when all we really care about is its sum. Instead, we could construct an iterator that successively yields prime numbers and compute a rolling sum; when the next prime number exceeds maximum, we'd know to stop and return the current sum.
Not surprisingly, iterators can save a tremendous amount of memory and time when working very large sequences or datasets, or even infinite ones. In fact, iterators enable us to represent an infinite number of items with finite memory. Even with smaller sequences or datasets, using iterators can help us write more efficient code. Here are a few scenarios that come to mind where using an iterator may be advantageous:
Performing computations on a stream of data Accessing certain items of a sequence without first storing the entire sequence Processing large files or datasets in manageable chunks (buffering) Performing computations on a sequence without knowing if all items in the sequence will be needed Building custom data samplers or random number generators The biggest drawback to using iterators is the numerous lines of code required to implement a complete iterator protocol, even if the resulting iterator is rudimentary (like myIteratorClone). Of course, Python wouldn't live up to its reputation if it didn't offer a few ways to simplify the process of producing iterators, conceivably down to a single, straightforward line of code! We'll learn about these shortcuts and some handy built-in iterators in an upcoming blog post.
For more read refer : http://inmachineswetrust.com/posts/understanding-iterators/

- 79
- 1
- 1
- 11