Why to override hashCode() method?

The hashCode() method is inherited by every class from the java.lang.Object class. Before we actually talk about why we need to override the hashCode() method, let us first talk about what is a hashcode? and where it is used?
You can think of hashcode as kind of an object ID number, it isn’t necessarily unique. If you actually try to print an object of a class using System.out.print(), we get the name of the class followed by a @ symbol, followed by the unsigned hexadecimal representation of the object’s hashcode(assuming that the class didn’t override the toString() method).
for example-

public class test
{
public static void main(String args[]) {
test obj = new test();
System.out.println(obj);
}
}

output-
test@1a46e30
Hashcodes are typically used to increase the performance of collections of data. Collections such as HashMap and Hashtable use the hashcode value of the object to determine how the object should be stored in the collection and use the same hashcode value to locate the object in the collection. To understand how the hashcodes work lets look at the following scenario which talks about how some of the collections might use hashcodes.

Let us imagine there are a set of buckets named in the following way “1-10″,”11-20” and so on. Suppose given the word “Test”, we try to find out the hashcode value in the following way. Replace the letters with their corresponding integer value with A being 1, B being 2 and so on. Then we add all the integer values to get the hashcode of that particular word. In this case the value of “Test” will be calculated in the following way

Test = T(20)+E(5)+S(19)+T(20)[Hashcode Algorithm] = 64 [Hashcode] so Test will be put into the bucket marked “60-70”. The flaw of this algorithm is that two words can be put into the same bucket. So while retrieving the word we use the same hashcode algorithm to find out the bucket in which the word is stored and then use the equals() method to compare and get back the correct value. For retrieving the values it is necessary to override both the hashCode() and equals() methods in situations where the objects of a particular class are used as keys in the Collection classes.
Implementing the hashCode():

public class test {
int x;
public test(int x){
this.x=x;
}
public boolean equals(Object o){
test obj=(test)o;
if(obj.x==this.x){
return true;
}
else{
return false;
}
}
public int hashCode(){
return (x*19)+13;
}
public static void main(String args[]) {
test obj = new test();
System.out.println(obj);
}
}

So the hashCode() method will tell the collection on how to store the object and equals() method tells you that if two object’s x values are equal then they are logically equal. For more information on equals() method check out Why to override equals() method?

The general contract of hashCode is:
* Whenever it is invoked on the same object more than once during an execution of a
Java application, the hashCode method must consistently return the same integer,
provided no information used in equals comparisons on the object is modified. This
integer need not remain consistent from one execution of an application to another
execution of the same application.
* If two objects are equal according to the equals(Object) method, then calling the
hashCode method on each of the two objects must produce the same integer result.
* It is not required that if two objects are unequal according to the
equals(java.lang.Object) method, then calling the hashCode method on each of the two
objects must produce distinct integer results. However, the programmer should be aware
that producing distinct integer results for unequal objects may improve the
performance of hashtables.