• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Taken from Java Ranch SCJP mock test



 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry, but can you tell me what is the implementation here that would print 2
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic