• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashSet Duplicate element ?

 
Soumya Ranjan Mohanty
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


How to modify the above code such that the hs.add(ws2); returns false. i.e ws1 and ws2 will be considered as equal(because both have the same value of s) and cannot be added into HashSet .
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to override both Object#equals and Object#hashCode. Can you figure out why and how ?
 
Soumya Ranjan Mohanty
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am getting the same output.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that comparing String values with "==" is right ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you are not overriding hashCode. Try to use the @Override annotation on your method to see what happens.
 
Elchin Asgarli
Ranch Hand
Posts: 222
Chrome Eclipse IDE Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:Are you sure that comparing String values with "==" is right ?


Theoretically it could, since he is hardcoding "john" and JVM must cache it, so == could work.
 
Elchin Asgarli
Ranch Hand
Posts: 222
Chrome Eclipse IDE Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even thought == on strings could theoretically work, you better use .equals on them, so instead of doing this.getS()==(((WrapStr)o).getS()) do this.getS().equals((((WrapStr)o).getS())). This approach is safer and not JVM-dependent
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this code:

You will get them....
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not return count.hashCode() from your hashCode method? A fixed value is terrible for performance when used with a HashMap or HashSet.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Why not return count.hashCode() from your hashCode method? A fixed value is terrible for performance when used with a HashMap or HashSet.


I just follow her coding, in that she uses a fixed value.
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah right. Then my comment applies to her code
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I modified WrapStr program such that the equals() method uses equals() instead of == and it also outputs size as 3 instead of 2. Code is below, please guide.


 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll quote Christophe just because he's 100% right:
Christophe Verré wrote:Also, you are not overriding hashCode. Try to use the @Override annotation on your method to see what happens.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:I'll quote Christophe just because he's 100% right:
Christophe Verré wrote:Also, you are not overriding hashCode. Try to use the @Override annotation on your method to see what happens.


We use these annotations in EJB, What is the need of it here? It's just another overriding?
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just try it, you'll see how useful @Override can be.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!