Why should the Java Iterator interface be implemented as an inner class?

I recently read the 3rd Edition Java Tutorials book. This indicates the implementation of the inner class, as the image shows.

The third paragraph says: "The Stack class itself should not implement the Iterator interface, because ...".

I cannot find the reasons why the Stack class should not implement Iterator. The indicated cause is NOT insightful.

Could you explain this?

Inner classInner class

+8
java iterator inner-classes
source share
5 answers

In essence, an iterator in terms of state - it needs to know where it points in the collection. This is not part of the collection itself - and this explanation is correct ... it is entirely possible to have two independent iterator objects, iterating over the same collection object. How would you formulate this if the collection itself implemented the Iterator interface? This is possible (for example, creating a new instance of the collection, which in turn contained a link to the original collection), but it would be very ugly.

There are some problems here:

  • Data collection
  • The cursor located inside the collection

Separate problems => separate classes.

The easiest way to convince yourself of this is probably to try to implement your own collection - and then have multiple iterators. For example, you can try:

List<String> foo = new MyListImplementation<String>(); foo.add("a"); foo.add("b"); // The enhanced for loop uses Iterable/Iterator for non-arrays for (String x : foo) { for (String y : foo) { System.out.println(x + " " + y); } } 

This should print:

 aa ab ba bb 

Try to implement it without having two classes, and see how you do it, given the separation of problems.

+8
source share

The stack does not have to implement Iterator itself, because then you can only have one iterator at a time, and iterating over the stack will change the stack.

In the latest release, note that the nested class has a currentItem field. This field should be in "Stack" and will change when next () is called. Iteration over a collection should not modify the collection.

The first problem is more serious: suppose two people iterate over the stack (or one method wants to create two iterators over the stack). Then if iterator() returns this , the two iterators will be the same. A call to next() on one would move the other. Chaos.

+4
source share

A Stack cannot be its own Iterator , because Stack supports more than one Iterator.

You might want to repeat the operation on the stack more than once. These iterations can occur at different times or even at the same time. Multiple iterations at the same time explicitly require multiple objects. Several iterations at different times require multiple objects, because the Iterator interface does not support returning to the beginning.

+2
source share

There are two reasons I can think of from the head.

Several types of iterators

You may need several types of iterators that iterate differently. For example, iterator forward and reverse iterator (iterations from the end of the container to the beginning).

Multiple Iterator Instances

If you have an algorithm with multiple passes and / or nested loops, each loop may want its own iterator to keep track of where it is in the container, regardless of other iterators.


It would be difficult, if not impossible, to support this functionality with the Iterator interface implemented in the Stack class.

+1
source share

To add to the discussion, the inner class will have access to the personal data of the Stack class, so this way the Stack class will be processed by the client programmer as an object or several objects (iterator (s)), and yet this object will be able to access the class and provide a separate iterate over the collection.

0
source share

All Articles