• 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

Equals and Hashcode

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to know any real time scenario example where hashcodes are equal for two objects but the objects are not equal
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about taking the String class as an example... The hash code is an integer value, so that is what? a bit more than 4 billion possible values? And as for a string, well, it can contain much much much more than 4 billion values...

So, isn't it logical that you will run out of hash codes and have to repeat. And when you repeat, you will have a case where the hash codes are equal, but the strings are not.

Henry  

PS.... welcome to the ranch.
 
harsha karanam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry,

that was a nice example where we run in to the scenario of having same hashcodes for two objects but the objects are different
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sreeharsha Karanam wrote:I would like to know any real time scenario example where hashcodes are equal for two objects but the objects are not equal


I assume you meant "real-world scenario"

You should note that the Java API Documentation for hashCode also states (emphasis mine):

It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.


If the range of values that your object uses to calculate hashCode() lends itself well to efficient calculation of distinct integer results, then you should do that if you're going to use your objects with classes that use hashCode().
 
harsha karanam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Junilu ,

The case where the objects are unequal using equals method , my understanding is if objects are unequal in that case the hashcodes of the objects must be unequal, consider the below example.

Country.Java:


Test.java:



Output:
Both countries are not equal
Country 1 hashcode:70793526
Country 2 hashcode:-1514818128

this case i was able to get different hashcodes , i believe this is the point you were trying to highlight in the post.

But in the other case  i was not able to have an example that s how i started this thread ;)
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sreeharsha Karanam wrote:The case where the objects are unequal using equals method , my understanding is if objects are unequal in that case the hashcodes of the objects must be unequal, consider the below example.


No, that is not correct. The hashCode contract specifically says "It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results."

As Henry showed in his example with Strings, it is NOT required that the results be distinct integers if equals() is true false for two objects. However, if it is at all practically possible to make it so, then you can potentially improve the performance of hashTables that use your objects. This is because you will eliminate the need to perform any collision resolution logic in the hashTable.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, since your Country object uses only the String name field to calculate its hashCode, I don't really think you gain anything by following Joshua Bloch's hashCode formula versus just doing something like this:
 
harsha karanam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
now i get it that its not necessary to product distinct integer results if objects are not equal but if it produces it enhances performance, but am curious to know whether we can make such sample code where objects are not equal using equals method but hashcode produces same integer results for the objects.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to be very careful when designing classes like Country and implementing equals() for it. Carefully consider the context(s) in which it will be used and whether the semantics of the equals() logic applies in all those contexts.

The fact that your Country class includes a population field, something that changes over time, can present some tricky situations.  For example, are these two objects always going to be equals()?

If you go with the equals() method that you wrote, the actual result will be true. This may or may not be valid, depending on the context this kind of code will be used.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sreeharsha Karanam wrote:now i get it that its not necessary to product distinct integer results if objects are not equal but if it produces it enhances performance, but am curious to know whether we can make such sample code where objects are not equal using equals method but hashcode produces same integer results for the objects.


Henry already explained all that to you in his example with Strings.  If the range (all the possible values) that your object's hashCode can take exceeds the range of all possible integers, then you can expect to have collisions for the hashcode. We don't purposely make the hashcodes collide; it all depends on the semantics of equals() and the values you use to calculate hashCode. The algorithm to calculate hashCode must also be relatively efficient, so you don't want to spend too many cycles trying to generate a unique value. You have to take both aspects into consideration when calculating hashCode. Don't just follow a formula just because that's what some book or article recommends. Understand what you're doing and why the formula is something that is reasonable to use.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another possible scenario where you have to carefully consider the semantics of equals() that you implement for your objects:

I would ask questions like:
- Would it be better perhaps to use the international country code?
- Would it be better to separate the population field from the Country class, and put it into something like a Census class that has country, year, and population? This type of class is what Peter Coad refers to as a Moment-Interval Archetype.

I think I'm starting to digress to a deeper design discussion though so I'll stop here.
reply
    Bookmark Topic Watch Topic
  • New Topic