This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How is this printing 3, i have overridden the hashcode and equals method in here Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How is this printing 3, i have overridden the hashcode and equals method in here" Watch "How is this printing 3, i have overridden the hashcode and equals method in here" New topic
Author

How is this printing 3, i have overridden the hashcode and equals method in here

Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
Taken from Java Ranch SCJP mock test


Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18548
    
  40



The method that you want to override is hashCode() -- with a capital C. Just like the method that you called in the contained string object.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18548
    
  40



And this is probaly not correct. You want your HashTest instances to compare to other HashTest instances -- not String instances.

Henry
Charles Chikito
Ranch Hand

Joined: May 22, 2009
Posts: 76
I guess hs.add(s2); won't take place because HashSet (or Set in general ) don't allow duplicate.

_Charles
krishna kanthgaru
Greenhorn

Joined: Feb 12, 2007
Posts: 17
Henry's method worked (if you were expecting last SOP to print 2)

public boolean equals(Object object){
return this.str.equals(((HashTest)object).str);
}

Henry Wong wrote:

And this is probaly not correct. You want your HashTest instances to compare to other HashTest instances -- not String instances.

Henry


no signature ... i am not a celebrity yet
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18548
    
  40

Henry's method worked (if you were expecting last SOP to print 2)


Actually, it is *not* my method. I was merely quoting the OP and explaining some of the issues.... which BTW, it is also technically not a valid implementation. The equal() contract requires that they be symmetric, which obviously, it is not.

As for this method...



It also has problems. It doesn't do a type check before the cast. If you notice the OP example, he places different types of objects into the set. This method would cause a cast exception, if the HashTest object had to share the bucket with a string object.

Henry
Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
Thanks Henry, but can you tell me what is the implementation here that would print 2
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

its simple.... change your equals to this


Why it printed 3 because Strings and Wrappers have overridden equals so no duplicates were allowed. Hashtest equals method was not properly implemented. It compared Hashtest.str with an object that you passed. It has already been explained above.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
 
 
subject: How is this printing 3, i have overridden the hashcode and equals method in here
 
Similar Threads
help sought in Collections ?
HashSet Size
Please explain the output
hashset
HashSet Duplicate element ?