• 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

Hashing

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey there,

I was on this site a couple of days ago and I was asking about hashing and equals().
Unfortunately IO received 2 anmswers which completely contradicted each other.
My question is this:

Lets say the equials method has 2 variables 'a' and 'b'.
What must the hashing method have?
A) Use at most both 'a' and 'b' and no more?
B) Use both 'a' and 'b' and also more variables?
C) Use only 'a' or use only 'b' or use none at all?

Basically -- genereally how must the hash method behave with respect to the variables used in the equals() method?
Use all of them/some of them/none of them?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You must start with the hashCode() and equals() contracts:
If x.equals(y) is true, then x.hashCode()==y.hashCode()

This can be done in different ways, depending on the definition of equals() for your class.

For example, if you have:
public boolean equals(Object g) { return false; }
then any definition of hashCode() is OK.

If you have:
class C {
public boolean equals(Object g) { return g != null && g instanceof C; } }
then there is one good definition of hashCode():
public int hashCode() { return 0; /* or any constant value */ }
this version may not be efficient, but it almost always works.

As I said, it depends on the definition of equals().
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Equals/hashCode method contracts paraphrased.
reflexive: x.equals(x) is true for all x, except null
symmetric: x.equals(y) is true for all x, y where y.equals(x) is true
transitive: x.equals(z) is true for all x, y, z where x.equals(y) is true and y.equals(z) is true
consistent: x.equals(y) returns the same value on multiple invocations if the object has the same state with respect to how it specifies equality.
x.equals(null) is false for all x
x.hashCode() == y.hashCode for all x, y where x.equals(y)

The actual method contracts are specified in the API Specification for java.lang.Object:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html
This is the definitive source of the contracts - anything else (including my paraphrasing) is an extrapolation from truth and should be approached with caution.

This software will assist you in meeting these contracts:
http://www.jtiger.org/

Specifically, the assertions package:
http://www.jtiger.org/javadoc/org/jtiger/assertion/EqualsMethodContract.html
http://www.jtiger.org/javadoc/org/jtiger/assertion/HashCodeMethodContract.html
 
reply
    Bookmark Topic Watch Topic
  • New Topic