What are the benefits of the Iterator interface in Java?

后端 未结 16 1922
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 14:35

I just learned about how the Java Collections Framework implements data structures in linked lists. From what I understand, Iterators are a way of traversing th

相关标签:
16条回答
  • 2020-12-04 14:59

    An interesting paper discussing the pro's and con's of using iterators:

    http://www.sei.cmu.edu/pacc/CBSE5/Sridhar-cbse5-final.pdf

    0 讨论(0)
  • 2020-12-04 15:00

    You ask: "Why are the methods hasNext(), next() and remove() not directly coded to the data structure implementation itself?".

    The Java Collections framework chooses to define the Iterator interface as externalized to the collection itself. Normally, since every Java collection implements the Iterable interface, a Java program will call iterator to create its own iterator so that it can be used in a loop. As others have pointed out, Java 5 allows us to direct usage of the iterator, with a for-each loop.

    Externalizing the iterator to its collection allows the client to control how one iterates through a collection. One use case that I can think of where this is useful is when one has an an unbounded collection such as all the web pages on the Internet to index.

    In the classic GoF book, the contrast between internal and external iterators is spelled out quite clearly.

    A fundamental issue is deciding which party conrols the iteration, the iterator or the client that uses the iterator. When the client controls the iteration, the iterator is called an external iterator, and when the iterator controls it, the iterator is an internal iterator. Clients that use an external iterator must advance the traversal and request the next element explicitly from the iterator. In contrast, the client hands an internal iterator an operation to perform, and the iterator applies that operation to every element ....

    External iterators are more flexible than internal iterators. It's easy to compare two collections for equality with an external iterator, for example, but it's practically impossible with internal iterators ... But on the other hand, internal iterators are easier to use, because they define the iteration logic for you.

    For an example of how internal iterators work, see Ruby's Enumerable API, which has internal iteration methods such as each. In Ruby, the idea is to pass a block of code (i.e. a closure) to an internal iterator so that a collection can take care of its own iteration.

    0 讨论(0)
  • 2020-12-04 15:00

    Just M2C, if you weren't aware: you can avoid directly using the iterator interface in situations where the for-each loop will suffice.

    0 讨论(0)
  • 2020-12-04 15:00

    Iterator simply adds a common way of going over a collection of items. One of the nice features is the i.remove() in which you can remove elements from the list that you are iterating over. If you just tried to remove items from a list normally it would have weird effects or throw and exception.

    The interface is like a contract for all things that implement it. You are basically saying.. anything that implements an iterator is guaranteed to have these methods that behave the same way. You can also use it to pass around iterator types if that is all you care about dealing with in your code. (you might not care what type of list it is.. you just want to pass an Iterator) You could put all these methods independently in the collections but you are not guaranteeing that they behave the same or that they even have the same name and signatures.

    0 讨论(0)
  • 2020-12-04 15:03

    it is important to keep the collection apart from the pointer. the iterator points at a specific place in a collection, and thus is not an integral part of the collection. this way, for an instance, you can use several iterators over the same collection.

    the down-side of this seperation is that the iterator is not aware to changes made to the collection it iterates on. so you cannot change the collection's structure and expect the iterator to continue it's work without "complaints".

    0 讨论(0)
  • 2020-12-04 15:04

    Well it seems like the first bullet point allows for multi-threaded (or single threaded if you screw up) applications to not need to lock the collection for concurrency violations. In .NET for example you cannot enumerate and modify a collection (or list or any IEnumerable) at the same time without locking or inheriting from IEnumerable and overriding methods (we get exceptions).

    0 讨论(0)
提交回复
热议问题