• Post Reply Bookmark Topic Watch Topic
  • New Topic

Iterable Interface & Collection Interface Relation  RSS feed

 
Vaibhav Gargs
Ranch Hand
Posts: 58
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collection interface extends the Iterable interface. And, Iterable interface has just one method: iterate(). I am just wondering why do we have one separate interface as Iterable? Why this method was not defined in Collection interface itself?
 
Campbell Ritchie
Sheriff
Posts: 54495
150
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It used to be all one interface. In 2004 (Java5), they introduced the for‑each loop, and the right operand is either an array or something which can produce an Iterator. It wasn't possible to fit Collection and all that sort of thing into the right of the for‑each loop, because it would make it impossible for somebody to write a different interface and use it there. So what they did was move the iterator() method out of Collection and into a new interface. That means, if Collection extends Iterable, it restores the original state of Collection and doesn't break any other code.
 
Stephan van Hulst
Saloon Keeper
Posts: 7376
130
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because not all things that you can iterate over are collections.

Collection implies something that has a definite size. It is a collection of things of which all elements are known at a given moment in time. This restriction does not apply to an Iterable. When you iterate over an Iterable, its elements may be generated on the fly. Here's an example:

You can iterate over all the Peano numbers, but you can not put them all in a collection because there are infinitely many of them.
 
Vaibhav Gargs
Ranch Hand
Posts: 58
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: It wasn't possible to fit Collection and all that sort of thing into the right of the for‑each loop, because it would make it impossible for somebody to write a different interface and use it there.


Thank you Cambell & Stephan.

@Campbell, can you please explain the quoted statement; i am getting it hard to grasp it.
 
Campbell Ritchie
Sheriff
Posts: 54495
150
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Stephan has already answered that question.
Stephan van Hulst wrote:Because not all things that you can iterate over are collections. . . . You can iterate over all the Peano numbers, but you can not put them all in a collection because there are infinitely many of them.
If you made the right argument of a for‑each loop a Collection, it would be impossible to iterate anything else with it. It would not be possible to program the javac tool to recognise everything which might go on the right there. Imagine you programmed it for random numbers, prime numbers, odd numbers, Fibonacci numbers and the Oxford English Dictionary (which doesn't have a determined size), and forgot that somebody would want to iterate Peano numbers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!