• 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

doubts in equals and hashCode

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why the hashcodes are equal ???
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic