1

I am still quite new to Java and today I try to understand the use of iterators.
So I have a few questions:

  1. Why would I need to implement Iterable at all, it only gives a method to create a new iterator for a collection, but I could just as well use a normal, or enchanced for-loop if I wanted to get all elements. I already read this question: What is the Iterable interface used for? and much more, but they kind of just say that it allows you to iterate through all your elements, which leads back to my question.

  2. If I implement Iterable, should I implement Iterator too?
    Because this one actually provides methods that could be useful to overwrite.
    For example if my class had a boolean whether I want my objects in a loop or not, I could write

    public boolean hasNext() {
    if(loop)
        return true;
       //other things
    }
    
  3. If I do like said in 2., will an enchanced for-loop use these overwritten methods?
    For example with the mentioned loop attribute above: Would the for-loop run infintely if it was true?

I hope someone can help me better understand this whole concept.

Community
  • 1
  • 1
Big_Chair
  • 2,781
  • 3
  • 31
  • 58
  • 1
    How will you use a for-each on your class if it *doesn't* implement Iterable? – Dave Newton Mar 14 '13 at 18:39
  • Duplicate http://stackoverflow.com/questions/89891/what-are-the-benefits-of-the-iterator-interface-in-java – Vaishak Suresh Mar 14 '13 at 18:41
  • For future questions, please ask only one question at a time. Linking between SO questions is fine, but one SO question should only contain one question. Imagine what happens if someone answers just your 1st and 2nd questions, but leaves the 3rd one open - if you accept the answer, future visitors may not see that the 3rd sub-question is still open, and if you don't accept it, it looks like the answers to the first two sub-questions were incorrect or dissatisfying. – O. R. Mapper Mar 14 '13 at 18:45
  • @O.R.Mapper ok sorry for that – Big_Chair Mar 14 '13 at 19:01

1 Answers1

4

If you are defining a collection, you are implementing an Iterable. For example, if you want to create a custom list, you will typically make it subclass AbstractList, which itself extends AbstractCollection, which implements Collection, which implements Iterable. So your list will be "de facto" an Iterable.

But if you subclass AbstractList, you won't have to implement the iterator yourself, since this method would already be implemented there. That said, you may want to provide a specialized iterator for your needs.

When you implement Iterable (if you really want to), you return an Iterator. The collection itself should not implement Iterator, only the returned iterator. Look at the AbstractList source, for an example to see how it is done.

When you are using an enhanced for-loop, you ARE using an Iterable. An enhanced for loop is just syntactic sugar for a loop on the iterator returned by iterator(). It means the compiler transforms your code to use the iterator. This is why you have to implement Iterable for the collection to be used in an enhanced for-loop.

Cyrille Ka
  • 15,328
  • 5
  • 38
  • 58