Jeff Verdegan wrote:
Mansukhdeep Thind wrote:
Not confused Jeff. I meant that before Iterable came into being, one would have to implement the mandatory methods of Collection interface including Iterator<E> iterator.
Still have to do that. Nothing has changed.
Jeff Verdegan wrote:
Mansukhdeep Thind wrote: (2 actually, remove() is optional)
You still have to implement it. Optional just means it doesn't have to actually remove anything.
Jeff Verdegan wrote:This is only an educated guess, but I would expect that there were 2 main reasons for adding the Iterable interface.
Jeff Verdegan wrote:Note the difference:
Iterable : A class that can be iterated over. That is, one that has a notion of "get me the first thing, now the next thing, and so on, until we run out."
Iterator : A class that manages iteration over an iterable. That is, it keeps track of where we are in the current iteration, and knows what the next element is and how to get it.
Point noted. Iterable was more of an after thought I believe. If we had only 1 interface Iterable right from when Collection Framework was introduced way back in 1.2 release, which had all the 4 methods that the 2 interfaces have together, wouldn't that have been a better design choice? Their purpose is linked. You have to implement Iterator if you make your class Iterable. So why not follow design principle of cohesion and club these two? What do you think Jeff?
Even if Iterable had been there from the beginning, we'd still want Iterator. They are two distinct responsibilities. We want our collections to be iterable--we want to be able to iterate over them. But we DON'T want the collections themselves to be responsible for their own iteration.
The way it is now, with two different interfaces, is a good approach. It just would have been nice if they'd separated them at the beginning.
~ Mansukh
Mansukhdeep Thind wrote:
Jeff Verdegan wrote:
Mansukhdeep Thind wrote:
Not confused Jeff. I meant that before Iterable came into being, one would have to implement the mandatory methods of Collection interface including Iterator<E> iterator.
Still have to do that. Nothing has changed.
But I created my CustomArrayList class and did not implement Collection interface but directly implemented Iterable and Iterator interfaces.
Jeff Verdegan wrote:
Mansukhdeep Thind wrote: (2 actually, remove() is optional)
You still have to implement it. Optional just means it doesn't have to actually remove anything.
I really don't get what you're saying here. If you're still not clear on something, please start a separate thread with a very small code sample to indicate what you're talking about?
, I pointed this out. No more is it necessary to implement the Collection interface if you have to make your class Itearable. Earlier(before 1.5) you had to do it. There was no other way of generating an iterator for my custom list if I wanted. Hope you understand what I am saying now.Still have to do that. Nothing has changed.
~ Mansukh
Mansukhdeep Thind wrote:I am talking about how I manually implemented my own CustomArrayList class without implementing Collection interface. I simply implemented java.lang.Iterable which is a super-interface of all Collection interfaces (except Map of course). Then I went on to implement Iterator...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
On top of spaghetti all covered in cheese, there was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|