Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Set Collection

 
Umesh Vajpai
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Could anyone answer me why Concrete classes like HashSet and Linked HashSet implements set ,not directly Collection ,even though there is no additional methods in Set interface.
 
Shilpa Tonk
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Umesh Vajpai wrote:Hi,

Could anyone answer me why Concrete classes like HashSet and Linked HashSet implements set ,not directly Collection ,even though there is no additional methods in Set interface.


Collection interface is divided into two main interfaces named set and list.
Both having some differences based on functionality.
HashSet and Linked HashSet does not directly implement collection interface just because of this feature.
 
Umesh Vajpai
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shilpa Tonk wrote:
Umesh Vajpai wrote:Hi,

Collection interface is divided into two main interfaces named set and list.
Both having some differences based on functionality.
HashSet and Linked HashSet does not directly implement collection interface just because of this feature.



Hi Shilpa,

Set And Collection having same methods then how can both differ in functionalities?
 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Set And Collection having same methods then how can both differ in functionalities?

Both Set and Collection are interfaces. So the methods of these interfaces do not have any functionality.
1) Some of the methods (like add method) are implemented in concrete class and
2) some of the Collection methods (like addAll method) are implemented in AbstractCollection class.
Because of point 1 some methods have different functionality in concrete classes.
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the Javadoc page of java.util.Set. You're right, it doesn't specify any new methods. It does refine the contract for several methods; for instance, Collection.add specifies that the return value is true if the collection changed. Set.add specified that the return value is true if the element wasn't in the Set already. In other words, by implementing Set you specify that each element will occur only once. Collection doesn't have that requirement.

I have no idea though why Set declares methods clear(), isEmpty(), iterator(), or either of the toArray methods. Their contracts specify nothing that wasn't in the contracts for the same methods in Collection. Then again, List also declares all methods of Collection. Maybe they just copied the Collection interface for all sub interfaces, added a few methods (if any) and fixed some Javadoc comments.
 
Umesh Vajpai
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,

Thanks to put light on this.I personally what think ,There would be some reason behind to declare Set interface with same methods
as in Collection.Classes which implements Set could had directly implement Collection.Then why there was need of Set interface.As in java there is reason behind everything so I posted question over here.Can put more light about this thought.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48976
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another reason for having a Set<E> interface is that when you read the code, you can tell that the declared variable represents a set, rather than an unspecified collection.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic