Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I accept a parameter of Iterable<T> vs. Collection<T> in Java?

What are the considerations of using Iterable<T> vs. Collection<T> in Java?

For example, consider implementing a type that is primarily concerned with containing a collection of Foos, and some associated metadata. The constructor of this type allows one-time initialisation of the object list. (The metadata can be set later.) What type should this constructor accept? Iterable<Foo>, or Collection<Foo>?

What are the considerations for this decision?

Following the pattern set forth by library types such as ArrayList (which can be initialised from any Collection, but not an Iterable) would lead me to use Collection<Foo>.

But why not accept Iterable<Foo>, given that this is is sufficient for the initialisation needs? Why demand a higher level of functionality (Collection) from the consumer, than what is strictly necessary (Iterable)?

like image 938
Daniel Fortunov Avatar asked Jul 21 '09 15:07

Daniel Fortunov


People also ask

Should I use Iterable or List?

Personally I would use Iterable<T> if that allows you to do everything you want it to. It's more flexible for callers, and in particular it lets you do relatively easy filtering/projection/etc using the Google Java Collections (and no doubt similar libraries).

Do all collections implement Iterable?

The Collection interface extends Iterable , so all subtypes of Collection also implement the Iterable interface.

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.


2 Answers

Many of the collection types existed before Iterable<T> (which was only introduced in 1.5) - there was little reason to add a constructor to accept Iterable<T> as well as Collection<T> but changing the existing constructor would have been a breaking change.

Personally I would use Iterable<T> if that allows you to do everything you want it to. It's more flexible for callers, and in particular it lets you do relatively easy filtering/projection/etc using the Google Java Collections (and no doubt similar libraries).

like image 109
Jon Skeet Avatar answered Sep 20 '22 13:09

Jon Skeet


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...

like image 26
Adam Paynter Avatar answered Sep 21 '22 13:09

Adam Paynter