File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How Set avoid to store duplicate value. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How Set avoid to store duplicate value." Watch "How Set avoid to store duplicate value." New topic
Author

How Set avoid to store duplicate value.

Prateek Kumar Singh
Greenhorn

Joined: Jan 21, 2011
Posts: 29

Hi Experts,

I want to know to know the step by step operation perform by java if we are going to add duplicate value in Set.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
Well, since the source code for the standard library is freely available, why not look at the various implementations of the Set interface directly.

Thats a great way to learn Java.

Bill
Krishna Kanth
Ranch Hand

Joined: Oct 23, 2006
Posts: 49
    
    1

Good Idea, William. Timely reminder for me, since i used to do this earlier but dropped.


Prateek, Set ( which internally uses a map) compares hashvalue && (reference equality || object equality) before saving a new value.
Look up rt.jar like William suggested using a Java Decompiler, for more details.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Please read this:-
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
…which appears at the top of this forum. It often does not help to give people too complete an answer too soon, but I think after 1½ weeks it is unlikely to do any harm
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
krishna kanth wrote:Look up rt.jar like William suggested using a Java Decompiler, for more details.

There's no need to do that. There's a src.zip in the JDK directory that contains the source code.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

krishna kanth wrote:Prateek, Set ( which internally uses a map) compares hashvalue && (reference equality || object equality) before saving a new value.

Erm...no. HashSet does that; but Sets don't necessarily have to be hashed.

For a Set that extends AbstractSet, I suspect you'll find that the default implementation is much more likely to be something like:HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Krishna Kanth
Ranch Hand

Joined: Oct 23, 2006
Posts: 49
    
    1

Yes, Winston. i was looking at HashSet implementation from rt.jar. Thanks for correcting.
However, just verified that AbstractSet did not implement add method.

I suggested java decompiler, since source code may be difficult to find for non-standard jar files, which is why i prefer java decompiler over source code.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

krishna kanth wrote:However, just verified that AbstractSet did not implement add method.

Hmmm. Probably because, like most 'mod' operations, it's optional.

I have to admit being quite surprised (although I'm sure you're right), but it's probably to ensure structural independence. And my general point was that "duplicate" maintenance is far more likely to be based around contains() than add().

Interesting stuff though.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How Set avoid to store duplicate value.