File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jacob Sonia
Ranch Hand
Posts: 179
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Taken from Java Ranch SCJP mock test


 
Henry Wong
author
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
Henry Wong
author
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess hs.add(s2); won't take place because HashSet (or Set in general ) don't allow duplicate.

_Charles
 
krishna kanthgaru
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry, but can you tell me what is the implementation here that would print 2
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic