Saturday, August 21, 2010

Equals and hashCode mystery resolved

Equals and hashCode mystery resolved




Hi,

Try executing the code in the attached files first by commenting the hashcode() method in Test1.java and then by uncommenting.

The output differs.

In first case the objects are equal based in the criteria defined in the overridden equals method but when u try to put these two “equal” objects in a hashtable it allows you inspite of the fact the keys (objects) are equal.

Here: both objects are equal but hashtable consider them different so allows two entries.

In second case when we have overridden the hashcode() method for objects of class Test1 uncommented then the hashtable doesn’t allow to put the equal objects twice.

Here: both objects are equal and hashtable consider them equal because of overridden hashtable method so allow only one entry

import java.util.Hashtable;
public class RunTest
{
        public static void main (String args[])
        {
               Test1 test1= new Test1("start", 10);
               Test1 test2= new Test1("start", 10);
               Hashtable ht = new Hashtable();
                System.out.println("test1.hashCode=" + test1.hashCode());
                System.out.println("test2.hashCode=" + test2.hashCode());
                System.out.println("test1.equals(test2) = " + test1.equals(test2));
                if(test1.hashCode()!=test2.hashCode())
                {
                    System.out.println("hashCodes are different --> means Objects are unequal");
                    if(test1.equals(test2))
                    {
                        System.out.println("Please note test1.equals(test2) returns " + test1.equals(test2));
                        System.out.println("Still it considers the objects to be unequal");
                    }
                    System.out.println("and adds it to hashTable");
                }
                else
                {
                    System.out.println("hashCodes are same");
                    System.out.println("means");
                    System.out.println("Objects can be equal or unequal");
                    System.out.println("Now it'll check equals method");
                    System.out.println("test1.equals(test2) = " + test1.equals(test2));
                    if(test1.equals(test2))
                    {
                        System.out.println("Since equals method returns true, it'll not allow 2 equal elements to be added");
                    }
                }
               ht.put(test1,"first");
               ht.put(test2,"second");
               System.out.println("the size of ht is :" + ht.size());
        }
}


public class Test1
{
        String s;
        int t ;
        public Test1(String s, int t)
        {
               this.s= s;
               this.t=t;
        }
 
        public boolean equals(Object obj)
        {
               if(this == obj)
               {
                       return true;
               }
               if(!(obj instanceof Test1))
               {
                       return false;
               }
               Test1 test = (Test1)obj;
 
               return (s.equalsIgnoreCase(test.s) && t==test.t);
        }
 
        public int hashCode()
        {
               return (this.s.hashCode() + this.t);
        }
}

1 comment:

  1. This is just not enough to know about hashCode....

    There is lot more that one should know about hashCode..

    See this...

    http://mindprod.com/jgloss/hashcode.html

    ReplyDelete