I was asked in an interview what is the advantage of using iterator over for loop or what is the advantage of using for loop over iterator?

Can any body please answer this?

  • 2
    A for-each loop uses an iterator under the covers. The difference is just readability (and therefore maintainability). – Dawood ibn Kareem Mar 8 '14 at 10:13
  • 1
    Well; why did you say Iterator? – Boris the Spider Mar 8 '14 at 10:16
  • 3
    Nothing forces you to answer immediately. You can reason about the different kinds of collections, what each code does behind the scene, and show your ability to reason, even if you can't formulate a definitive answer. That's what is important: being able to think. I would start my answer with: "well, I don't know. I guess it depends on what you want to do, and what the kind of collection is. There must be a good reason to use both, but it must depend on the situation. Let's imagine what happens with an ArrayList. Then with a LinkedList. etc. etc." – JB Nizet Mar 8 '14 at 10:25
  • 3
    I agree entirely with @JBNizet. Saying I don't know it much better than picking a random option - that gives me the impression that you would do that while programming; pick the first thing that comes to mind without thinking. You could try and explain your knowledge in the area than say that you don't know enough to give an informed answer. – Boris the Spider Mar 8 '14 at 10:26
  • 2
    Both current answers are incomplete. First of all, there are 2 kinds of for loops, which behave very differently. One uses indices (which isn't always possible), and the other one uses an Iterator behind the scenes. So you in fact have 3 loops to compare. Then you can compare them in different terms: performance, readability, error-proneness, capability. An Iterator can do things that a foreach loop can't. But an Iterator is more dangerous and less readable. And using indices to access elements is only doable with Lists, but is not efficient if it's a linked list. So there's no "best" way. – JB Nizet Mar 8 '14 at 10:34


    Thing t = list.get(i);

This kind of loop isn't always possible. For example, Lists have indices, but Sets don't, because they're unordered collections.

The other one, the foreach loop uses an Iterator behind the scenes:

for (Thing thing : list) {

This works with every kind of Iterable collection (or array)

And finally, you can use an Iterator, which also works with any Iterable:

for (Iterator<Thing> it = list.iterator(); it.hasNext(); ) {
    Thing t = it.next();

So you in fact have 3 loops to compare.


An Iterator can do things that a foreach loop can't. For example, you can remove elements while you're iterating, if the iterator supports it:

    Thing t = it.next();
    if (shouldBeDeleted(thing) {

Lists also offer iterators that can iterate in both directions. A foreach loop only iterates from the beginning to an end.

But an Iterator is more dangerous and less readable. When a foreach loop is all you need, it's the most readable solution. With an iterator, you could do the following, which would be a bug:


A foreach loop doesn't allow for such a bug to happen.

Using indices to access elements is slightly more efficient with collections backed by an array. But if you change your mind and use a LinkedList instead of an ArrayList, suddenly the performance will be awful, because each time you access list.get(i), the linked list will have to loop though all its elements until the ith one. An Iterator (and thus the foreach loop) doesn't have this problem. It always uses the best possible way to iterate through elements of the given collection, because the collection itself has its own Iterator implementation.

My general rule of thumb is: use the foreach loop, unless you really need capabilities of an Iterator. I would only use for loop with indices with arrays, when I need access to the index inside the loop.

  • When you use an Iterator to remove one or more elements in a collection, this is safe to do. When you use a for-each or for loop, removing is either not allowed or causes a bug because when removing while looping over the collection the indexes of each element in the collection changes. Therefore you should use a reverse for loop, or use an iterator to prevent introducing a bug. See also this answer. – user504342 Apr 5 '16 at 9:53
  • 2
    that "ith" you said, is a challenge in English language. – M D P Oct 29 '16 at 6:42
  • 1
    Maybe he can replace it with "i-th"? – Miroslav Todorov Mar 13 '20 at 9:28

Iterator Advantage:

  • Ability to remove elements from Collections.
  • Ability to move forward and backward using next() and previous().
  • Ability to check if there more elements or not by using hasNext().

Loop was designed only to iterate over a Collection, so if you want just to iterate over a Collection, its better to use loop such as for-Each, but if you want more that that you could use Iterator.

  • 2
    And with a ListIterator you can also add and start iteration at an arbitrary point. – Boris the Spider Mar 8 '14 at 10:17
  • @Salah: why iterator can remove elements while iterating the collection? – logbasex Jun 29 '20 at 10:39

The main difference between Iterator and the classic for loop, apart from the obvious one of having or not having access to the index of the item you're iterating, is that using Iterator abstracts the client code from the underlying collection implementation, allow me to elaborate.

When your code uses an iterator, either in this form

for(Item element : myCollection) { ... }

this form

Iterator<Item> iterator = myCollection.iterator();
while(iterator.hasNext()) {    
    Item element = iterator.next();    

or this form

for(Iterator iterator = myCollection.iterator(); iterator.hasNext(); ) {
   Item element = iterator.next();

What your code is saying is "I don't care about the type of collection and its implementation, I just care that I can iterate through its elements". Which is usually the better approach, since it makes your code more decoupled.

On the other hand, if you're using the classic for loop, as in

for(int i = 0; i < myCollection.size(); i++) {
   Item element = myCollection.get(i);

Your code is saying, I need to know the type of collection, because I need to iterate through its elements in a specific way, I'm also possibly going to check for nulls or compute some result based on the order of iteration. Which makes your code more fragile, because if at any point the type of collection you receive changes, it will impact the way your code works.

Summing it up, the difference is not so much about speed, or memory usage, is more about decoupling your code so that is more flexible to cope with change.


if you access to data by number (e.g. "i"), it is fast when you use array. because it goes to element directly

But, other data structure (e.g. tree, list), it needs more time, because it start from first element to target element. when you use list. It needs time O(n). so, it is to be slow.

if you use iterator, compiler knows that where you are. so It needs O(1) (because, it start from current position)

finally, if you use only array or data structure that support direct access(e.g. arraylist at java). "a[i]" is good. but, when you use other data structure, iterator is more efficient

  • So I should say it depends on the requirement. – rocking Mar 8 '14 at 10:19
  • +1 for pointing out the disadvantages of using a loop by index on Java Collections. It's worth pointing out that an enhanced for loop uses an Iterator under the hood. – Boris the Spider Mar 8 '14 at 10:20
  • Your other data structure examples aren't helpful - an ArrayList provides indexed access. Maybe update to include specific Java collections? (e.g. ArrayList vs LinkedList) – Richard Miskin Mar 8 '14 at 10:22

Unlike other answers, I want to point another things;

if you need to perform the iteration in more than one place in your code, you will likely end up duplicating the logic. This clearly isn’t a very extensible approach. Instead, what’s needed is a way to separate the logic for selecting the data from the code that actually processes it.

An iterator solves these problems by providing a generic interface for looping over a set of data so that the underlying data structure or storage mechanism — such as an array- is hidden.

  • Iterator is a concept not an implementation.
  • An iterator provides a number of operations for traversing and accessing data.
  • An iterator may wrap any datastructure like array.
  • One of the more interesting and useful advantages of using iterators is the capability to wrap or decorate another iterator to filter the return values
  • An iterator may be thread safe while a for loop alone cannot be as it is accessing elements directly. The only popular thread-safety iterator is CopyOnWriteArrayList but it is well known and used often so worth mentioning.

This is from the book that it is https://www.amazon.com/Beginning-Algorithms-Simon-Harris/dp/0764596748


I stumbled on this question. The answer lies to the problems Iterator tries to solve:

  • access and traverse the elements of an aggregate object without exposing its representation
  • define traversal operations for an aggregate object without changing its interface

Your Answer

