Monday 15 April 2019

                                         hashCode() method of object class in java:


For every object jvm will generate a unique number which is nothing but hashCode.
Jvm will using hashCode while saving objects into hashing related data structures like HashSet, HashMap, and Hashtable etc.
If the objects are stored according to hashCode searching will become very efficient (The most powerful search algorithm is hashing which will work based on hashCode).
If we didn't override hashCode() method then Object class hashCode() method will be executed which generates hashCode based on address of the object but it doesn't mean hashCode represents address of the object.
Based on our programming requirement we can override hashCode() method to generate our own hashcode.
Overriding hashCode() method is said to be proper if and only if for every object we have to generate a unique number as hashcode for every object.


class Student
{
public int hashCode()
{
return 100;
}
}
It is improper way of overriding hashCode() method because for every object we are generating same hashcode.


class Student
{
int rollno;
public int hashCode()
{
return rollno;
}
}
It is proper way of overriding hashcode() method because for every object we are generating a different hashcode.



class Test
{
int i;
Test(int i)
{
this.i=i;
}
public static void main(String[] args){
Test t1=new Test(10);
Test t2=new Test(100);
System.out.println(t1);
System.out.println(t2);
}
}
Object==>toString() called.
Object==>hashCode() called.

In this case Object class toString( ) method got executed which is internally calls Object class hashCode( ) method.



class Test{
int i;
Test(int i){
this.i=i;
}
public int hashCode(){
return i;
}
public static void main(String[] args){
Test t1=new Test(10);
Test t2=new Test(100);
System.out.println(t1);
System.out.println(t2);
}}
Object==>toString() called.
Test==>hashCode() called.


In this case Object class toString( ) method got executed which is internally calls Test class hashCode( ) method.


class Test
{
int i;
Test(int i)
{
this.i=i;
}
public int hashCode(){
return i;
}
public String toString()
{
return i+"";
}
public static void main(String[] args){
Test t1=new Test(10);
Test t2=new Test(100);
System.out.println(t1);
System.out.println(t2);
}
}
Output:
10
100


In this case Test class toString() method got executed.

Note :
if we are giving opportunity to Object class toString() method it internally calls hashCode() method. But if we are overriding toString() method it may not call hashCode() method.
We can use toString() method while printing object references and we can use hashCode() method while saving objects into HashSet or Hashtable or HashMap.



class TestHashCode
{
int i;
TestHashCode(int i)
{
this.i=i;
}
public static void main(String[] args){
TestHashCode t1=new TestHashCode(10);
TestHashCode t2=new TestHashCode(100);
System.out.println(t1);
System.out.println(t2);
}
}







class TestWithHashCodeOnly{
int i;
TestWithHashCodeOnly(int i){
this.i=i;
}
public int hashCode(){
return i;
}
public static void main(String[] args){
TestWithHashCodeOnly  t1=new TestWithHashCodeOnly(10);
TestWithHashCodeOnly t2=new TestWithHashCodeOnly(100);
System.out.println(t1);
System.out.println(t2);
}}






class TestWithHashAndtoStringMethod
{
int i;
TestWithHashAndtoStringMethod(int i)
{
this.i=i;
}
public int hashCode(){
return i;
}
public String toString()
{
return i+"";
}
public static void main(String[] args){
TestWithHashAndtoStringMethod t1=new TestWithHashAndtoStringMethod(10);
TestWithHashAndtoStringMethod t2=new TestWithHashAndtoStringMethod(100);
System.out.println(t1);
System.out.println(t2);
}
}



1 comment: