Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Internal Implementation of Set (Collection) in java.

 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49751
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49751
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Jason has described is a HashSet. TreeSets use a completely different way to insert their elements.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49751
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic