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?

overexchange
  • 15,768
  • 30
  • 152
  • 347

3 Answers3

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.

Tim
  • 12,318
  • 7
  • 50
  • 72
  • `java.util.ServiceLoader` is a real life example of a class implementing `Iterable` without being a `Collection`. – Holger Oct 29 '18 at 09:43
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

Community
  • 1
  • 1
Nisheeth Shah
  • 600
  • 1
  • 9
  • 22
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.

Naman Gala
  • 4,670
  • 1
  • 21
  • 55
  • 1
    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? – overexchange Jul 23 '15 at 05:30
  • 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. – Naman Gala Jul 23 '15 at 05:33
  • 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. – Naman Gala Jul 23 '15 at 05:37
  • 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. – overexchange Jul 23 '15 at 05:40
  • 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. – Naman Gala Jul 23 '15 at 05:44
  • My point is, they could have placed all the method names in `Iterable`, instead of creating another interface `Iterator`. – overexchange Jul 23 '15 at 05:48
  • If both of them are dependent on each other then it can be done. But in this case Iterable is dependent on Iterator, but Iterator is independent of Iterable. You will not see any use of iterable in iterator interface. And this is the choice of the coder of this interfaces. – Naman Gala Jul 23 '15 at 05:52