When should one consider implementing Iterable<T> as opposed to having a collection as an instance field? What are the benefits/consequences?
When should one consider implementing Iterable<T> as opposed to having a collection as an
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Implementing Iterable supports encapsulation. There is (usually) no need for the users of your class to know whether you are using a linked list or a hash table or what have you.
You could change the implementation details without having to modify any code that uses your class. Perhaps you want to switch between collections depending in the number of items, for instance.
You are also in control of what the user can and cannot do. If you give direct access to your collection then they may modify it without your knowledge, a very bad idea.