Pages

Tuesday, July 24, 2012

hashCode() method in java

public int hashCode() : Returns a hash code value for the object.

- As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects.
- If two object are equal a/c to the equals() method, then calling hashCode() on those object must produce the same hash value.
- It's not mandatory that if hash values of two object are equal, then the two objects should be equal.

It's generally said,

Key of the Hashtable must implement hashCode() and equals() methods. Because,
Let's say:
h - hash function
o1 - object1
o2 - object2

It's a possible scenario where objects o1 and o2 have same hash values, although these two objects are unequal.
h(o1) = h(o2) = 1671711, although o1 != o2;

So that means two objects are stored at the same location, as adjacent elements(seperate chaining). So by using equals() method it'll compare the key and returns the desired value.

it's possible to retrieve original hash code although you overriden the original one,
System.identityHashCode(obj) will return an int value for the passed object.

AS per JDK docs:
The general contract of hashCode is:
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
 

It is not required that if two objects are unequal according to the java.lang.Object.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.

A simple example:

Mobile.java


package com.hashcode.samples;

public class Mobile {

 private String maker;
 private double megaPixel;
 private double price;
 private int makeYear;

 public Mobile(String maker, double megaPixel, double price, int makeYear) {
  this.maker = maker;
  this.megaPixel = megaPixel;
  this.price = price;
  this.makeYear = makeYear;
 }

 public String getMaker() {
  return maker;
 }

 public double getMegaPixel() {
  return megaPixel;
 }

 public double getPrice() {
  return price;
 }

 public int getMakeYear() {
  return makeYear;
 }

 public String toString() {
  return "Maker : "   this.maker   "\t"   "Camera : "   this.megaPixel
      "MP"   "\t"   "Price : "   this.price   "\t"
      "Year of manufacturing :"   this.makeYear;
 }

 public int hashCode() {
  return makeYear;
 }
 
 public boolean equals(Object obj) {
  boolean flag = false;
  Mobile m = (Mobile) obj;
  if(m.makeYear == this.makeYear) {
   flag = true;
  }
  return flag;
 }

}

CheckHash.java
package com.hashcode.samples;

public class CheckHash {
 public static void main(String[] args) {
  Mobile m1 = new Mobile("Nokia",3.2,6500,2001);
  System.out.println("m1 : \n" m1);
  System.out.println("\n" "hashcode of m1 : " m1.hashCode());
  System.out.print("Original hashCode of m1 :");
  int orghash = System.identityHashCode(m1);
  System.out.println(orghash);
  
  Mobile m2 = new Mobile("Nokia",3.2,6500,2002);
  System.out.println("\n" "hash code of m2: " m2.hashCode() "\n" "m1.equals(m2)->" m1.equals(m2));
  
  Mobile m3 = new Mobile("Nokia",3.2,6500,2002);
  System.out.println("\n" "hash code of m3: " m3.hashCode() "\n" "m2.equals(m3)->" m2.equals(m3));
 }

}

output : 
m1 :
Maker : Nokia    Camera : 3.2MP    Price : 6500.0    Year of manufacturing :2001

hashcode of m1 : 2001
Original hashCode of m1 :1671711 //your hashcode may varies;

hash code of m2: 2002
m1.equals(m2)->false

hash code of m3: 2002
m2.equals(m3)->true

1 comment:

  1. Good Article.It would be more clear if you write about
    What is hashing?
    Why do we need to ovrride hashCode()
    what is equals, how hashCode and equals are related..
    Thanks

    ReplyDelete