Hi,
I wonder why the Collection.addAll()
method only accepts other Collection
s but not Iterable
s. Why is that?
Any similar method to do that for Iterable
s?
Hi,
I wonder why the Collection.addAll()
method only accepts other Collection
s but not Iterable
s. Why is that?
Any similar method to do that for Iterable
s?
Presumably because the Collection
interface was introduced in Java 1.2 whereas Iterable
appeared only in 1.5, and changing the interface would break all existing implementations.
Basically because an iterable may never en ( that is, getNext() return true forever )
Also, to keep congruency, you may think a Collection may add all the elements of another collection, but, an iterable is not necesarily a collection ( it may be anything, like the a ResultSet wrapper for instance )
Because not all Iterable
objects are java.util.Collection
.
E.g. java.sql.SQLException
is iterable.
public class SQLException extends java.lang.Exception
implements Iterable<Throwable> {
How would collections interpret it? It wouldn't return a collection of it's parameterized type (of the collection).
When in doubt, always check Guava (or Commons):