4

Here is my understanding on significance of using Iterable and Iterator in pre 1.8 java.

1)

java.util.AbstractList is Iterable because it implements,

Iterator<T> iterator();, which is a contract for any class to be Iterable.

2)

java.util.AbstractList implements,

Iterator<T> iterator(); by creating an instance level inner class,

private class Itr implements Iterator<E> { ... } 

that implements hasNext, next and remove methods.

private class ListItr extends Itr implements ListIterator<E>{..} is just an extra facility over above iterator for List type implementations.

3)

Is this the only purpose of these two interfaces Iterable & Iterator, to enable any object to be iterable? Please let me know, if there is any other purpose of these two interfaces?

6
  • 1
    What do you mean by signify their existence? Commented Jul 23, 2015 at 4:53
  • stackoverflow.com/questions/6863182/… Commented Jul 23, 2015 at 4:54
  • stackoverflow.com/questions/5836174/java-iterator-and-iterable Commented Jul 23, 2015 at 4:54
  • @Eran their existence of usage Commented Jul 23, 2015 at 4:55
  • You described how Iterable is implemented in AbstractList. Other than that, it's still not clear what you're asking. Commented Jul 23, 2015 at 4:59

3 Answers 3

4

You are right stating what these interfaces are used for. Indeed Iterable declares that the objects of a class implementing it may be iterated over, by providjng an Iterator specific to these objects. Having them is necessary because how exactly the object should be iterated depends on its internal implementation, and an Iterator is therefore specific to a given "collection" class.

Having said that, it is worth noting that although these interfaces are formally a part of Java Collections framework, they can be applied to other cases. Given an imaginary API to read CSV files for example, one can declare a CsvFile class to implement an Iterable<List<String>> and iterate over lines in a file with a dedicated Iterator<List<String>> which will read lines from the file one-by-one and split them into a List of Strings to return it from next().

Another important purpose of these interfaces is a language feature known as "for each" loop - only objects of a class implementing Iterable can be iterated with it. So, given an example from above about CsvFile API, it will also enable something like:

CsvFile csvFile = new CsvFile(pathToCsvFile); for (List<String> record : csvFile) { doSomethingWithIt(record); } 

As "for each" loop is purely a language feature, compiler will expand it to use an Iterator as usual.

P.S. Just because it hurts my eyes, I'd like to add that in the example above I would also suggest implementing an AutoCloseable for the CsvFile and using it with try-with-resources.

Sign up to request clarification or add additional context in comments.

1 Comment

java.util.ServiceLoader is a real life example of a class implementing Iterable without being a Collection.
0

java.util.Collection interface extends to java.util.Iterable. Iterable has a method that produces the iterator. If any class implements iterable, it has an iterator method that produces java.util.Iterator.

Please refer to this post

1 Comment

I don't think OP is asking about this implementation, instead asking any other use case other then this.
0

If you check out interface Iterable it has only one method that is Iterator<T> iterator();. So there is no other possible use case for implementing Iterable interface other than providing iterator method.

If you see the documentation of Iterator interface, in See Also section you will find Collection, ListIterator, Iterable. Collection and ListIterator are by default Iterable as they internally extend Iterable. So Iterable is used in conjunction with Iterator.

7 Comments

Another points is, Behavior responsibility is divided between Iterable and Iterator interface. Can't single interface something like Iterable(say) hold the responsibility having all 4 methods hasNext next remove and iterator? I mean, am not getting the thought process for having two interfaces, Iterable and Iterator instead of one. What is the thought process in designing this way?
It is just code division. You can see Iterator is used with Iterable and other interface extends Iterable only, so the functionality is kept directly on Interable.
And also Iterator is independent of Iterable. So the functionality is divided. Iterable's functionality is to provide iterator Object, and iterator's functionality is to provide next, remove, hasNext functions.
other interface, you mean, interface Collection implements Iterable? yes, but inner class of AbstractList which is Itr, has to implement Iterator to make it complete.
Yes because you want outside world to use Iterator functionality also you want to hide the implementation into inner class. Consider everyone using there own method names, it would create a mess, so they are implementing Iterator to implement their logic as well as giving freedom of using directly as Iterator object instead Itr object.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.