• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't understand the contract of hashCode

 
sonu raj
Ranch Hand
Posts: 43
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

The above lines are taken from javadoc of hashcode of Object class. Can anyone show the condition where two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects produce same or equal integer results.

e.g.

1. String s1=new String("hello");
2. String s2=new String("hi");
3. System.out.println(s1.equals(s2)); //output false
4. System.out.println(s1.hashCode()); //output some hashcode1
5. System.out.println(s2.hashCode()); //output some hashcode2

I want to know the condition when line 3 is false and 4 and 5 have same output
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sonu raj wrote: Can anyone show the condition where two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects produce same or equal integer results.

You can override hashCode() method in your class as following than it will return same integer for all its instances either equal or unequal.

Note: It is not an appropriate way to override but it is a legal override example of hashCode().
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sonu raj wrote: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.

The above lines are taken from javadoc of hashcode of Object class. Can anyone show the condition where two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects produce same or equal integer results.

e.g.

1. String s1=new String("hello");
2. String s2=new String("hi");
3. System.out.println(s1.equals(s2)); //output false
4. System.out.println(s1.hashCode()); //output some hashcode1
5. System.out.println(s2.hashCode()); //output some hashcode2

I want to know the condition when line 3 is false and 4 and 5 have same output

Welcome to CodeRanch!

You can write your own class to demonstrate that specific part of contract. e.g. an Employee class - whose equals method returns true only if employee id is matching, and whose hashCode method always returns 0 (or 1, or 6 or whatever).

Here, you are not breaking the contract - which says that if equals returns true for 2 objects, then hashCode must return true for both objects.

However, the method is pretty inefficient - meaning, there's only one possible output of hashCode - and thus, there will be only one 'bucket' for all Employee objects.

I hope this helps.
 
sonu raj
Ranch Hand
Posts: 43
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Very Much for your replies
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome.
 
Henry Wong
author
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The original question....

sonu raj wrote:I want to know the condition when line 3 is false and 4 and 5 have same output


does sound interesting. It shouldn't be too hard to recursively generate a ton of strings to try to find one with a matching hashcode. Of course, it may take a really long time to find this matching string ...

Henry
 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Henry Wong
author
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
The original question....

sonu raj wrote:I want to know the condition when line 3 is false and 4 and 5 have same output


does sound interesting. It shouldn't be too hard to recursively generate a ton of strings to try to find one with a matching hashcode. Of course, it may take a really long time to find this matching string ...


Hey, it wasn't that bad. The program is still running, but this is what I got so far....

Target String: "hello" with hashCode 99162322
Found: "hello"
Found: "helmP"
Found: "heln1"
Found: "hemMo"
Found: "hemNP"
Found: "hemO1"
Found: "hen01"
Found: "hfMlo"
Found: "hfMmP"
Found: "hfMn1"
Found: "hfNMo"
Found: "hfNNP"
Found: "hfNO1"
Found: "hfO01"
Found: "hg001"
Found: "iFllo"
Found: "iFlmP"
Found: "iFln1"
Found: "iFmMo"
Found: "iFmNP"
Found: "iFmO1"
Found: "iFn01"
Found: "iGMlo"
Found: "iGMmP"
Found: "iGMn1"
Found: "iGNMo"
Found: "iGNNP"
Found: "iGNO1"
Found: "iGO01"
Found: "iH001"


Henry
 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can generate at most 4294967296 different hash codes if you use ints. If you are using the 26 letters of the English alphabet only, upper- and lower‑case, you exhaust the possible population of hash codes by the time you reach 6 letters.
Work it out: 32 × log(2) ÷ log(52). You get about 5.6.
 
Winston Gutkowski
Bartender
Pie
Posts: 10087
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sonu raj wrote:The above lines are taken from javadoc of hashcode of Object class. Can anyone show the condition where two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects produce same or equal integer results.

As you can see it's not that easy for most of the foundation classes, because their designers read and applied the next sentence:
"the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables."

What you need to understand is why we have hashcodes. A good hashcode makes objects work well in hashed collections, and the results can be startling: Retrieving a single well-hashed key from a HashMap containing 1,000 of them could be several hundred times faster than retrieving the same key from a List, and is likely to be around 10 times faster than getting it from a TreeMap.

Many languages, from awk on up, use hashed collections for tons of things because they're so darn fast; but the bedrock of a hashed collection is a good hashcode; and for that, you don't want unequal objects producing the same hashcode.

HIH

Winston
 
sonu raj
Ranch Hand
Posts: 43
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Henry this is what I was looking for...thank you very much
 
sonu raj
Ranch Hand
Posts: 43
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Henry
I would like to know that how come you know about these Strings? Is there any way by which you can elaborate the concept of hashcode, diagrammatically?
 
Winston Gutkowski
Bartender
Pie
Posts: 10087
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sonu raj wrote:Is there any way by which you can elaborate the concept of hashcode, diagrammatically?

Maybe this page will help; it has several.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic