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

Appropriate hashCode method implements with equals method

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to find whether the hashCode method is appropriate to the given equals method? Any suggestion??? (I don't mean the contract between them) Thanks in Advanced!
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the appropriate equals & hashcode methods should use same instance variables in both methods.
 
Sheriff
Posts: 9704
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

M M kaimkhani wrote:the appropriate equals & hashcode methods should use same instance variables in both methods.


That's not necessary, the hashCode and equals contract can still be fulfilled with different instance variables. To fulfill the contract hashCode must be the same for equal objects. So even this fulfills the hashCode and equals contract
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:How to find whether the hashCode method is appropriate to the given equals method? Any suggestion??? (I don't mean the contract between them) Thanks in Advanced!


If you don't mean the contract, I'm not sure what you mean. If you want to find whether it is appropriate, drop your object in a HashTable and see if you can ever find it again. Then, drop 100,000 other objects into the same HashTable and see if you can find yours quickly enough.

The way it is explained in the books I studied is " a HashTable is a series of buckets each with zero or more objects." The hashcode determines which bucket you look in to get the object. The equals determines which object you pull out of the bucket. It is appropriate if your implementation of equals and hashcode makes it possible and efficient to find the right bucket and then the right object. If you have 200,000 members in your HashTable all with the same hashcode, then your code has to find the object using only the equals method in one big "bucket" of objects. If the members are spread out over 2000 buckets, your code just has to search through 100 items to find the right one with the equals method.

If you are not putting the object in a Hashed collection, then override hashcode becomes less important.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Couple of Rules, that I know,

1) If hashcode method is invoked on an object multiple times it should return the same value, unless the object's state is being modified in such a way that it affects the hashcode calculation.(Never use any random number in "hashcode"( e.g. Math.Random)).

2) If two objects are equal according to the "equals" method then both of these objects must return the same value for "hashcode".
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:How to find whether the hashCode method is appropriate to the given equals method? Any suggestion??? (I don't mean the contract between them) Thanks in Advanced!


There are some guidelines available for writing good HashCode function. You can use Joshu Recipe and can also try HashCodeBuilder. Both are listed here
HashCodeBuilder require that you need to use the same instance field that are used in Equal method.
 
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
Here is a good article which explains hashCode() and equals():

Hashing it out - Defining hashCode() and equals() effectively and correctly
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to All.....
Tim McGuire, Muhammad Ali Khojaye, Jesper Young Thanks a lot......
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic