Why in Java 2 interfaces: Iterable and Iterator?


In my opinion, they only add confusion. I understand their roles: Iterable says that, in principle, it is possible to iterate over class objects, and Iterator sets the methods for iteration themselves. But in my opinion, all this would look great in one interface. Which would both declare the class to be iterable and at the same time set methods for iteration.


One might think about this if the intended match between an Iterable and its Iterator was one-to-one . But the situation is different: an Iterable can at some point be managed by several Iterator once (possibly also in different threads!). That is, they are interfaces that represent two different entities: a collection and a collection traversal .

For this reason alone should separate them, because otherwise for simultaneous multiple rounds collection must always be wrapped in an iterator, and have two types of copy (for an iterator for the collection), and there are already visible in clear violation of SRP (Single Responsibility Principle, single responsibility principle)

Scroll to Top