What are the considerations of using Iterable
For example, consider implementing a type that is primarily concerned with contai
An Iterable
produces Iterator
objects. An Iterator
object, by definition, iterates. Notice, that the Iterator
interface makes no promise as to how many times next()
can be called before hasNext()
returns false
. An Iterator
could possibly iterate over Integer.MAX_VALUE + 1
values before its hasNext()
method returns false
.
However, a Collection
is a special form of Iterable
. Because a Collection
cannot have more than Integer.MAX_VALUE
elements (by virtue of the size()
method), it is naturally presumed that its Iterator
objects will not iterate over this many elements.
Therefore, by accepting a Collection
rather than an Iterable
, your class can have some guarantee over how many elements are being passed in. This is especially desirable if your class is itself a Collection
.
Just my two cents...