aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubts in equals and hashCode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubts in equals and hashCode" Watch "doubts in equals and hashCode" New topic
Author

doubts in equals and hashCode

sasmit sahu
Greenhorn

Joined: Apr 12, 2002
Posts: 9
In the java doc for JDK1.4 for hasCode method in Object class it 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. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

So does it mean that if equals returns false then it's not necessary that the hascode of the two object will be different??And if yes can someone give some example where equals will return false and the hascode will be same.
Sasmit
SCJP1.4
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Please check out Dan Chisholm's site. He has just uploaded a couple of questions in his Hashcode exam that are in relation to your question.
http://www.danchisholm.net/beta/collections.html


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Well, a trivial (and highly NON-recommended) example is this:
public boolean equals(Object other) {
return (this == other);
}
public int hashCode() {return 1;}
This meets the contract requirement (equal objects return equal hash codes), but the hash table will perform as poorly as possible, since every object will hash to the same value.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

One example that I can think of immediately.
Integer i = new Integer(7);
Long l = new Long(7);
i.equals(l) => returns false
i.hashCode() == l.hashCode() => returns true.
HTH,
- Manish
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
The hashCode value is of type int. Since the set of all int values is much smaller than the set of all values of type long it is necessary for a large number of unequal long values to share a single hashCode value. The same is true for type double values.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Isabel Wanderley
Ranch Hand

Joined: Aug 24, 2002
Posts: 42
Why the hashcodes are equal ???
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
There are more long values than there are int values. If you're going to map longs to ints, some will have to have the same value.
Suppose you have 256 marbles and you want to put them in 16 buckets. Obviously at least one bucket is going to contain more than one marble.
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

The only think that needs to be understood here is - "If two objects are equal as per the equals(Object obj) method, their hash codes will be same; but if they are not equal that DOES NOT mean that their hash codes will be different, they could still be same." In other words same hash codes does not imply that two objects are equal.
HTH,
- Manish
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubts in equals and hashCode