• 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

Which is best to use in hashcode?

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,
which is best scenario for hashcode?
A> Use two instance variable to calculate the hashcode.
B> Use only one instance variable to calculate the hashcode.

If in exam i get both the option with complex code which option should i choose?? According to me A should be appropriate. What do you think??
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A good hashCode implementation returns unique hash codes for different objects. So the following is a very inefficient implementation of hashCode method
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If this question qualifies to be appeared in exams as it is given here , then my answer will also be A.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is impossible to answer, because whatever is "best" depends on the meaning of the class for which you are implementing the hashCode() method and also on what "equality" means for objects of that class. You will not get vague questions like this on the exam.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, How to write good hashCode() implementation..Here is a small program
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually Saibabaa, while your equals method isn't necessarily wrong, it's better to compare using equals. I made some changed to your example, feel free to ask my why I made some of the changes:
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephan, I changed your code with 3 comments/questions. Could you please advice ?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) In general it's a good idea not to allow null values unless they have a special meaning. In this case, it doesn't make sense for a dog not to have a name, so we don't allow null. It's best to throw exceptions as soon as possible, so in this case we throw NullPointerException in the constructor. Now we don't have to worry in other methods that name could be null, so we don't have to check every time we use name.

2) Your version of the equals method was good, except for using == instead of equals. I'm just used to writing it this way. The only real difference is that I check first whether o == this. The method should work fine without it, but this is a small optimization, so the method returns quickly if the object is being compared to itself.

3) Yes, you are correct. Here is a small example that shows how you should implement hashCode and equals methods for a class that holds different kinds of member fields:
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice..Thank you Stephan
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great work Stephan van Hulst.
 
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic