• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How Set avoid to store duplicate value.

 
Prateek Kumar Singh
Ranch Hand
Posts: 30
Eclipse IDE Hibernate Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
1
Chrome Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48370
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10086
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Krishna Kanth
Ranch Hand
Posts: 49
1
Chrome Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10086
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic