Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubts in equals and hashCode

 
sasmit sahu
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Isabel Wanderley
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why the hashcodes are equal ???
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic