| Author |
HashSet Duplicate element ?
|
Soumya Ranjan Mohanty
Ranch Hand
Joined: Mar 07, 2010
Posts: 44
|
|
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
Joined: Nov 24, 2005
Posts: 14672
|
|
|
You have to override both Object#equals and Object#hashCode. Can you figure out why and how ?
|
[My Blog]
All roads lead to JavaRanch
|
 |
Soumya Ranjan Mohanty
Ranch Hand
Joined: Mar 07, 2010
Posts: 44
|
|
I am getting the same output.
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
|
Are you sure that comparing String values with "==" is right ?
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
|
Also, you are not overriding hashCode. Try to use the @Override annotation on your method to see what happens.
|
 |
Elchin Asgarli
Ranch Hand
Joined: Mar 08, 2010
Posts: 222
|
|
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.
|
Personal page, SCJP 6 with 91%, SCWCD 5 with 84%, OCMJD
|
 |
Elchin Asgarli
Ranch Hand
Joined: Mar 08, 2010
Posts: 222
|
|
|
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
Joined: Nov 04, 2009
Posts: 2066
|
|
Check this code:
You will get them....
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Why not return count.hashCode() from your hashCode method? A fixed value is terrible for performance when used with a HashMap or HashSet.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
Ah right. Then my comment applies to her code
|
 |
Sandra Bachan
Ranch Hand
Joined: Feb 18, 2010
Posts: 434
|
|
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.
|
Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
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
Joined: Nov 04, 2009
Posts: 2066
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Just try it, you'll see how useful @Override can be.
|
 |
 |
|
|
subject: HashSet Duplicate element ?
|
|
|