overexchange
overexchange

Reputation: 1

Iterable and Iterator in java

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?

Upvotes: 4

Views: 3855

Answers (3)

Tim
Tim

Reputation: 13088

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.

Upvotes: 4

Naman Gala
Naman Gala

Reputation: 4692

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.

Upvotes: 0

Nisheeth Shah
Nisheeth Shah

Reputation: 608

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

Upvotes: 0

Related Questions