The hashCode Method in java
The hashcode is an integer value attached to an object that, on one hand, specifies the uniqueness
of an object and, on the other hand, helps to store an object in a data structure and to retrieve it. To
calculate the hashcode for an object, the Object class offers the hashCode() method, which has the
following syntax:
public int hashCode()
The method calculates and returns a hashcode of type int for the object on which it is invoked.
When you write a class that will be used in a Map, you can override the hashcode() method to implement
the algorithm that you will use to calculate the hashcode, but you must follow the rules for
returning the hashcode, called the hashcode contract. The rules of the hashcode contract are listed
here:
Applications of HashCode:of an object and, on the other hand, helps to store an object in a data structure and to retrieve it. To
calculate the hashcode for an object, the Object class offers the hashCode() method, which has the
following syntax:
public int hashCode()
The method calculates and returns a hashcode of type int for the object on which it is invoked.
When you write a class that will be used in a Map, you can override the hashcode() method to implement
the algorithm that you will use to calculate the hashcode, but you must follow the rules for
returning the hashcode, called the hashcode contract. The rules of the hashcode contract are listed
here:
- If the hashCode() method is invoked multiple times on the same object during the execution
of an application, it must consistently return the same integer value each time. However, the
returned integer value can change from one execution of an application to another execution
of the same application - If two objects are equal according to the equals(…) method, then invoking the hashCode()
method on each of the two objects must return the same integer value for the hashcode. - If two objects are unequal according to the equals(…) method, it is not required that invoking
the hashCode() method on each of these two objects must return unequal integer values
as hashcodes. However, you should be aware that returning distinct integer values as hashcodes
for unequal objects may improve the performance of hashtables.
we have a form which has input fields first name , last name , age ,reg no etc.when user fill these information and on submit the data is stored in database. and want to show a message that the data is stored in database or not. in this scinario you can use hashcode method , calculate the hashcode for input fields ,if the data is stored in database show the user a message that the data is successfully saved and if the user again press the save data with the same information in input fields , show the user a message that nothing have been changed.this is jsut one applicaiton of hashcode in my mind , you will find a lot to use hashcode in real world problems.
Examples Code:
package
aurangzeb.utk.com;
public class HashTest{
public static void main(String[]
args) {
HashStore hs1 = new HashStore(89,
101);
HashStore hs2 = new HashStore(75,
101);
HashStore
hs3 = new HashStore(89, 101);
System.out.println("Hashcode
for hs1: " + hs1.hashCode());
System.out.println("Hashcode
for hs2: " + hs2.hashCode());
System.out.println("Hashcode
for hs3: " + hs3.hashCode());
if(hs1.equals(hs2)) {
System.out.println("hs1 is
equal to hs2");
}else {
System.out.println("hs1 is
not equal to hs2");
}
if(hs1.equals(hs3)) {
System.out.println("hs1 is
equal to hs3");
}else {
System.out.println("hs1 is
not equal to hs3");
}
}
}
package
aurangzeb.utk.com;
public class HashStore {
private int key = 0;
private int value = 0;
private int storeSize = 10;
HashStore(int key, int value) {
this.key = key;
this.value = value;
}
public boolean equals(Object obj) {
if (!(obj instanceof HashStore)) {
return false;
}
HashStore
hs = (HashStore) obj;
return (key == hs.key && value == hs.value);
}
public int hashCode() {
return key % storeSize;
}
}
Output:
Hashcode for hs1: 9
Hashcode for hs2: 5
Hashcode for hs3: 9
hs1 is not equal to hs2
hs1 is equal to hs3
Hashcode for hs2: 5
Hashcode for hs3: 9
hs1 is not equal to hs2
hs1 is equal to hs3
References: SCJP
