Write the hashCode() method of java.lang.Object
Anytime you override equals() you should also override hashCode(). The hashCode() method should ideally return the similar int for any two objects which compare equal and a different int for any two objects in which don't compare equal, where equality is defined through the equals() method. This is used as an index by the java.util.Hashtable class.
In the Car example equality is denoted exclusively by comparing license plates; thus only the licensePlate field is used to denoted the hash code. Since licensePlate is a String, and since the String class has its own hashCode() method, we can sponge off of that.
public int hashCode() {
return this.licensePlate.hashCode();
}
Other times you may require to use the bitwise operators to merge hash codes for multiple fields. There are also a variety of useful techniques in the type wrapper classes (java.lang.Double, java.lang.Float, etc.) in which convert primitive data types to integers that share the similar bit string. These can be used to hash primitive data types.