aspose file tools*
The moose likes Beginning Java and the fly likes Can't understand the contract of hashCode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can Watch "Can New topic
Author

Can't understand the contract of hashCode

sonu raj
Ranch Hand

Joined: Jul 31, 2012
Posts: 43

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

Joined: Jun 02, 2012
Posts: 187
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

Joined: Dec 08, 2010
Posts: 1509
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
sonu raj
Ranch Hand

Joined: Jul 31, 2012
Posts: 43

Thank You Very Much for your replies
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

You are welcome.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Joined: Oct 13, 2005
Posts: 39396
    
  28
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

Joined: Mar 17, 2011
Posts: 8008
    
  22

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
sonu raj
Ranch Hand

Joined: Jul 31, 2012
Posts: 43

Thank you Henry this is what I was looking for...thank you very much
sonu raj
Ranch Hand

Joined: Jul 31, 2012
Posts: 43

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

Joined: Mar 17, 2011
Posts: 8008
    
  22

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't understand the contract of hashCode