Powered by Blogger.

Tuesday, 10 June 2014

Hashcode & Equal Method

By Shambhu Kumar  |  03:16 No comments

Why always override hashcode() if overriding equals()?
In Java, every object has access to the equals() method because it is inherited from the Object class. However, this default implementation just simply compares the memory addresses of the objects. You can override the default implementation of the equals() method defined in java.lang.Object. If you override the equals(), you MUST also override hashCode(). Otherwise a violation of the general contract for Object.

Let See One example how it fails if hashCode is not implemented
public class Customer {
       private long crmID;
       private int nameSpace;

       public Customer(long crmID, int nameSpace) {
           this.crmID = crmID;
           this.nameSpace = nameSpace;

         public boolean equals(Object obj) {
           //null instanceof Object will always return false
           if (!(obj instanceof Customer))
             return false;
           if (obj == this)
             return true;
           return  this.crmID == ((Customer) obj).crmID &&
                   this.nameSpace == ((Customer) obj).nameSpace;
         /*public int hashCode(){
              return nameSpace; 
         public static void main(String[] args) {
           Map m = new HashMap();
           m.put(new Customer(23L,0),"Jeff Smith");
           System.out.println(m.get(new Customer(23L,0)));


If I comment the hashCode method it will return null. If uncomment the hashCode method it will return Jeff Smith.

For More details Check this.

Important point for HashCode

  1. A HashSet has a number of buckets. It uses the hashCode() to determine which bucket an element belongs in, and then within that bucket, it uses equals() to find if the element exists in that bucket or not.
  2. The hashcode for an object need not to be completely unique, only that the hashcode for two equal objects returns the same hashcode. 
  3. It's entirely legal to have two non-equal objects return the same hashcode. However, the more unique a hashcode distribution is over a set of objects, the better performance you'll get out of HashMaps and other operations that use the hashCode.
  4. A good hash algorithm minimises collisions, but doesn't need to guarantee they won't occur.
  5. it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
  6. The default implementations of those methods are hashCode() uses a system identity hashcode, which is probably it's memory location, but is unique in that the system will make an attempt using all "reasonably practical" attempts to make no two objects have the same system identity hashcode, and equals compares the memory locations of the two objects. 

Hashcode and equeal method must satisfy below conditions

  1. When 2 objects are equal then they should have same hashcode.
  2. If 2 object has same hascode, then they must call equals() on each other to see whether are they equal.
  3. If hashcode matches its not necessary that they both are equal.
  4. So its very important that when equals() method is overridden then, hashcode() method must also be overridden.
  5. HashSet when taking an object in checks where this object fits in the HashSet, then is there any object with the same hashcode, if there then both are taken into a bucket with the same hashcode as the label, then calling equals() on each other to see, if they are really equal.


Author: Shambhu Kumar

Hello, I am Author, decode to know more: In commodo magna nisl, ac porta turpis blandit quis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In commodo magna nisl, ac porta turpis blandit quis. Lorem ipsum dolor sit amet.


Recent Articles

© 2014 Learning Java. WP themonic converted by Bloggertheme9. Published By Gooyaabi Templates