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.
Joined: Jun 28, 2009
Thanks Henry, but can you tell me what is the implementation here that would print 2
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