aspose file tools*
The moose likes Beginning Java and the fly likes Set Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Set Collection" Watch "Set Collection" New topic
Author

Set Collection

Umesh Vajpai
Greenhorn

Joined: Mar 14, 2010
Posts: 26
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

Joined: Mar 15, 2010
Posts: 6
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

Joined: Mar 14, 2010
Posts: 26
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

Joined: Jun 02, 2009
Posts: 182

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.


A.A.Anbarasu
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Umesh Vajpai
Greenhorn

Joined: Mar 14, 2010
Posts: 26
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Set Collection