What are the considerations of using Iterable
For example, consider implementing a type that is primarily concerned with contai
Users of Spring Data JPA
will find that the Repositories
return collections of type Iterable
On projects I've worked on in the past that use Spring
, I've found that the need to operate on a Collection after retrieval has often dictated that Iterable
is used in the business layer rather than Collection
in order to select an Object T
from the collection.
All Collections are Iterable
(that is the interfaces which extend the Collection
interface, so not Map
!), so using Iterable
in the business layer is merely a case of referring to a Collection by its super type and still allows the use of for-each
to iterate.
If you need to manipulate the contents of a collection, a convenience method would allow you to populate a new Collection
, so you can make use of contains()
, remove()
, etc with the original collection data.
Alternatively, convenience methods are provided for this purpose by popular third party APIs such as Google Guava and Apache Commons.