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 .
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
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.
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
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.