Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: why does Collection.addAll can not accept Iterables?

I wonder why the Collection.addAll() method only accepts other Collections but not Iterables. Why is that?

Any similar method to do that for Iterables?

like image 548
Albert Avatar asked Sep 30 '10 15:09

Albert


People also ask

Are all Java collections iterable?

java collection Java iterable interface The Collection interface extends Iterable interface, so all subtypes of Collection implement the Iterable interface. This interface stands to represent data-structures whose value can be traversed one by one. This is an important property.

What is the purpose of an iterator when working with collections?

'Iterator' is an interface which belongs to collection framework. It allows us to traverse the collection, access the data element and remove the data elements of the collection.


1 Answers

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.

like image 54
Michael Borgwardt Avatar answered Oct 11 '22 22:10

Michael Borgwardt