File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Internal Implementation of  Set (Collection) in java. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Internal Implementation of  Set (Collection) in java." Watch "Internal Implementation of  Set (Collection) in java." New topic
Author

Internal Implementation of Set (Collection) in java.

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi Ranchers,
I have a issue with Set in Collection on java.

Please find the below code



A set displays Unique/Distinct elements in the output which is the expected behaviour. -- Fine..

I wanted to know what is the internal implementation of a set in java.i.e As to how it finds it is a duplicate element added and deletes it and gives only unique elements. (need suggestions) ??

Can anyone help me with this scenario please.???




Deepak Lal


When The Going Gets Tougher,The Tougher gets Going
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Go into your Java installation folder, where you find a file called src.zip. Unzip that and you find the source code for all the well-known Java classes.

But Set is an interface; it hasn't got an implementation. You will have to find one of its implementing classes.
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
Set is an interface implemented by Hashset, TreeSet etc. Actually set doesn't delete duplicate element instead it doesn't add the duplicate element to the set. Set has got add method which uses Object class equals method to compare if the element to be added is equal. If element is equal then add return false and element will not be added.


SCJP 1.5(97%) My Blog
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
As Campbell and Shivendra say, Set is just an interface and there are many different types of sets. Some ordered, some not, some linked, some not, some navigable, some not.

Sets use the "hashcode()" of the object to identify a "bucket" into which that object should go. Once they have the "bucket" they then check for object equivalence using "equals()" and if an equivalent object is found, the new one is not added (exactly as Shivendra states).

The fact that Sets (and Maps...) use "hashcode()" and "equals" mean that it is important you understand the rules of these methods and how to implement them if you intend to add your own classes to Collections.


SCJP6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
What Jason has described is a HashSet. TreeSets use a completely different way to insert their elements.
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
My mistake...I had assumed that a TreeSet would still use hashcode buckets for efficiency (fewer equivalent tests) as it also does not allow duplicates. The other difference is the ordering implemented by TreeSet.

Guess I should take your advice and open the code (or look elsewhere) for the specifics on TreeSet operation.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Well done, Jason, going to check. But don't go telling everybody what you find; people learn a lot better when they find out for themselves than being told something.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Internal Implementation of Set (Collection) in java.