• 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’s contract

 
Ranch Hand
Posts: 240
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
equals() and hashcode’s contract:”If two objects have the same hash code, they may or may not be equal.” Can you explain with an example?

thanks in advance
 
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Even though implementing hashCode() by returning a fixed value is a bad idea, it is a legal implementation of the hashCode() contract.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another very simple example.  Define the hash of a decimal number as the value of the last digit.  The two numbers: 10 and 20 then have the same hash value but are not equal.

Using the last digit as hash would allow a hashmap to have exactly 10 buckets for chaining the values to save.  
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Using the last digit as hash would allow a hashmap to have exactly 10 buckets for chaining the values to save.  


This is not true. Hash maps perform their own rehashing operation on the hash code of an object to spread them out as much as possible. Even if all objects return the same hash code, a small hash map will still place most of them in a separate bucket.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

small hash map will still place most of them in a separate bucket.


Not always possible.  If the hashmap has 10 buckets and the user adds 100 items to it, then ideally each bucket would have 10 elements.

I'm talking about the idea of hashmaps in general, not the java class.

 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the case of the Java class though, a hash map with 100 elements in it will use 256 buckets by default.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an example, which uses a class you're familiar with: the String class.

How many different String values is it possible to have? Well, first of all the length of a String object can be anything from zero all the way up to 2^31 - 1. And each character in a String can be any of 65536 different characters. I'll let you figure out how many different Strings there can be, but if you want to skip that step let me just say that the number you get is incredibly humongous.

And how many different hash codes can that incredibly humongous number of Strings have? Well, a hash code has to be a Java int, so there can only be 2^32 different hash codes. That number is a lot less than "incredibly humongous" and so it follows that many, many different String objects must have the same hash code. (Look up Pigeonhole Principle if that doesn't strike you as immediately obvious.)
 
Greenhorn
Posts: 8
Hibernate Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arun Singh Raaj wrote: If two objects have the same hash code, they may or may not be equal



Its depend on your requirement in application..
Like, i have tested with team pairs where i want same hash code for the object having same pair..

here, for Pair(a,b) and Pair(b,a) will give you same hash code..

where as default implementation of Pair class will give different hash code for Pair(a,b) and Pair(b,a)

and you can override you equals method also as per your requirement in application.

Hope, you will get clarification  
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arun Singh Raaj,

Cowgratulations, your topic has been published in August edition of our CodeRanch Journal.
 
Arun Singh Raaj
Ranch Hand
Posts: 240
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank so much all of you for your speedy response.
thank you Liutauras Vilda.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic