• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

roles of Collection, Iterator, Iterable

 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Split from this thread...

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.



OK. I thought when the specification says optional operation it means that can be left unimplemented. I read it here Is this guy wrong when he says "Collection interface is the exception..."

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.



Hmm. Loosely coupled and highly cohesive. Correct.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



If you implemented List or extended AbstractList or AbstractSeqeuentialList or LinkedList or ArrayList, then you implemented Collection because List extends Collection, etc. If you didn't implement or extend any of those things, then your class just happens to have the same name as Java's ArrayList but is not part of the collections hierarchy.

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?

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.



OK. I thought when the specification says optional operation it means that can be left unimplemented. I read it here Is this guy wrong when he says "Collection interface is the exception..."

If by "exception" he means that the language rules are different as far as what's required for implementing an interface, then, yes, he's wrong.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



Read this thread. 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. So, when you said that

Still have to do that. Nothing has changed.

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

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, now I think I see what you're saying, which is exactly what I also said earlier: With the introduction of Iterable, we can make classes that we can iterate over in a standard way without having to implement Collection.

Where I got confused was that, based on the name, I thought your ArrayList was a java.util.List implementation. That led to other misunderstandings about what points you were trying to make.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Well, in either case you have no choice about the latter. I guess the question you need to ask yourself is: do I need to add or remove things from my Iterable? If so, chances are you should be implementing Collection, because that's basically what it is.

Just as an example: I've created a very useful class called Bag (that actually exists in a few other languages; oddly enough not in Java), which represent an opaque bag of objects - ie, you can put something in and you can take something out, but when you take something out, you have no idea which one you'll get. Since I wanted to add and remove things, it seemed sensible to me to implement Collection rather than just Iterable.

However, if you have something like an array that's already got things in it and you simply want to be able to go through them, it's quite possible you only need to implement Iterable (mind you, Java more or less does that for you for arrays anyway).

My basic rules of thumb is: if I want my class to be the subject of a for-each loop, I implement Iterable; if I also want to add and/orr remove things, I make it a Collection (or some subclass).

But others may have other ideas.

Winston
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic