Tuesday, October 23, 2012

difference between equals() and hashCode()

public boolean equals(Object obj)

This method checks if some other object passed to it as an argument is equal to the object on which this method is invoked. The default implementation of this method in Object class simply checks if two object references x and y refer to the same object. i.e. It checks if x == y. This particular comparison is also known as "shallow comparison". However, the classes providing their own implementations of the equals method are supposed to perform a "deep comparison"; by actually comparing the relevant data members. Since Object class has no data members that define its state, it simply performs shallow comparison.

The equals method implements an equivalence relation:
-It is reflexive: for any reference value x, x.equals(x) should return true.
-It is symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
-It is transitive: for any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
-It is consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified.
For any non-null reference value x, x.equals(null) should return false.

public int hashCode()

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 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.

Hashcode example

public class HashCodeString {
public static void main(String args[]) {
Integer j = new Integer(4);
Long k = new Long(4);
Integer v = new Integer(3);
System.out.println("false is returned: " + j.equals(k)); // Result - false
System.out.println("Hash code of number 4 is: " + j.hashCode() //Result 4
+ "\nHash code of number 4 is: " + k.hashCode()); // 4
System.out.println("Method returns three: " + v.hashCode()); // 3
String baadshah = "latter in time there will be my empire every where";
System.out.println("Corresponding hash code value returned is: "
+ baadshah.hashCode()); // Result 342564105
}

}


No comments:

Post a Comment