• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collections- HashMap

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the advantage of overriding hashcode in Gogx in code below. We can do the exact same thing with cat which does not override Hashcode


Result:
Gogx key
null
null
null
Cat key

Please explain the importance of overriding the Hashcode in Gogx.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note: There is a bug in line 10 of your code. The method should have been named hashCode(), not hashcode(). Because it has the wrong name, it's not overriding hashCode() of class Object, and it will not be called.

You should use the @Override annotation to catch such mistakes:

And there is another bug, in line 15. You are comparing strings with == there. That does not work; use .equals() instead to compare strings.
 
Sarvarth Bhatnagar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my bad, Thanks Jesper! for the annotation suggestion
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apart from the inconcistent indentation and spacing, which makes your code very difficult to read, you have some bad style in that equals method.
 
reply
    Bookmark Topic Watch Topic
  • New Topic