Monday 28 October 2013

Importance of the hashCode() method in java:


 Importance of the hashCode() method in java:
More frequently used in hashCode() method in design patterns.In interview point of view mostly asked question is hashCode() method and its importance.Now we will see the hashCode() method and its working.

hashCode() method:
--->If we want to use a class object as a key in a hashing collection of java then that class must be override hashCode() method and equals() method.
--->hashCode() method belongs to java.lang.Object class
--->if we don't override a hashCode() method in a class then Object class hashCode() method will be called.It will returns the memory address of an object in the form of an integer.
--->wrapper classes and string classes of java are already overriden a hashCode() method and equals() method. So we can use these class object as key in collections.
Some important points to remember with respect to hashCode() method
i)If two references are referring same object then always their hash Code's are equal.---->true
example:
    String s1="Aa";
    String s2=s1;
     s1==s2 ------>true
     s1.hashCode()=2112
     s2 .hashCode()=2112
So if two references are refer same object then their hash Codes are equal.
we can find the hashCode of a  string by using the fallowing formula.
     String=number format 1st character*31 pow(n-1)+number format of 2nd character *31 pow(n-2)
     Aa=65*31 pow(2-1)+97*31 pow(2-2)
          =65*31+97*1  
          =2015+97
          =2112

ii)If two hash Codes are equal then they are referring one Object                              ----->false.
example: 
     String s1="Aa"
     String s2="Aa"
   s1==s2     ---false
    s1.hashCode()=2112
    s2.hashCode()=2112
above example hasCodes are equal but they are referring two different objects.

iii)If two objects are meaningfully equal then they are hash Code's are equal(by force)----->true
    
-->when we are creating  user defined class the it is our responsibility  to override hashCode() method and equals() method.

example:
     class Demo
      {
           private int x;
            public Demo(int x)
             {
                this.x=x;
               }
            @Override
               public boolean equals(Object o)
                {
                 if(o instanceof Demo&&(((Demo).x)==this.x))
                 {
                    return true;
                      }
                  else
                   {
                       return false;
                       }
                   }
            @Override
                   public int hashCode()
                    {
                      return x+50;
                     }
                }
               class Main
                {
                  public static void main(String args[])
                   {
                      Demo d1=new Demo(20);
                      Demo d2=new Demo(20);
                          if(d1==d2)
                           {
                             System.out.println("d1 and d2 are identically equal");
                            }
                            if(d1.equals(d2))
                            {
                           System.out.println("d1 and d2 are meaningfully equal");
                            }
                        System.out.println("d1.hashCode()");
                         System.out.println("d2.hashCode()");
                      }
                   }
output:
    d1 and d2 are meaningfully equal
    70
    70

Difference between the hashCode()  method and equals() method?
ans:
---> both methods are overriden ,when an object of a class wants to be used as a key in a collection.
--->To store the data in a hash bucket hashCode() method will be called.But to read the data from a hash bucket first  hashCode() method will be called and the equals()method is called.

             
About Admin of the Blog:

Krishna babu is the founder of Alljobupdates .He is a Tech Geek,SEO Expert,Web Designer and a Pro Blogger. Contact Him Here

Follow him @ Facebook

0 comments:

Enjoyed? Share Your View With Us