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

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)