First look at the object source code in the Hashcode method:
/**
* Returns A hash code value for the object. This method is
* Supported for the benefit of hashtables such as those provided by
* <CODE>JAVA.UTIL.HASHTABLE</CODE>.
* <p>
* The general contract of <code>hashCode</code> are:
* <ul>
* <li>whenever It is invoked on the same object more than once during
* An execution of a Java application, the <tt>hashCode</tt> method
* must consistently return the same integer, provided no information
* used in <tt>equals</tt> comparisons on the object is modified.
* This integer need not remain consistent from one execution of
* Application to another execution of the same application.
* <li>if Objects is equal according to the <tt>equals (Object) </tt>
* method, then calling the <code>hashCode</code> method on each of
* The objects must produce the same integer result.
* <li>it is <em>not</em> required that if the objects is unequal
* According to the {@link java.lang.object#equals (java.lang.Object)}
* method, then calling the <tt>hashCode</tt> method on each of the
* objects must produce distinct integer results. However, the
* Programmer should is aware that producing distinct integer results
* For unequal objects may improve the performance of hashtables.
* </ul>
* <p>
* As much as is reasonably practical, the Hashcode method defined by
* Class <tt>Object</tt> does return distinct integers for distinct
* objects. (This was typically implemented by converting the internal
* Address of the object into a integer, but this implementation
* Technique is isn't required by the
* Java<font size= "-2" ><sup>TM</sup></font> programming language.)
*
* @return A hash code value for this object.
* @see java.lang.object#equals (java.lang.Object)
* @see java.util.Hashtable
*/
public native int hashcode ();
As we can see from the comments above, hashcode () is called when the Equals method is called, which is when we rewrite equals () to rewrite hashcode () ...
We're looking at the implementation of Hashtable:
/**
* Returns the hash code value for this Map as per the definition in the
* Map interface.
*
* @see Map#hashcode ()
* @since 1.2
*/
// 计算Hashtable的哈希值
public synchronized int hashcode () {
/*
* This code detects the recursion caused by computing the hash code
* of a self-referential hash table and prevents the stack overflow
* that would otherwise result. This allows certain 1.1-era
* Applets with self-referential hash tables to work. This Code
* Abuses the Loadfactor field to do double-duty as a hashcode
* In progress flag, so as not to worsen the space performance.
* A Negative load factor indicates that hash code computation is
* in progress.
*/
int h = 0;
if (count = = 0 | | Loadfactor < 0)
return h; Returns Zero
loadfactor =-loadfactor; Mark hashcode computation in progress
entry[] tab = table;
for (int i = 0; i < tab.length; i++)
For (Entry e = tab[i]; E! = null; e = e.next)
H + = E.key.hashcode () ^ E.value.hashcode ();//否则,返回“Hashtable中的每个Entry的key和value的异或值 的总和”。
loadfactor =-loadfactor; Mark hashcode computation complete
return h;
}
If two objects are equal according to the Equals (object) method, calling the Hashcode method on each object in both objects must produce the same integer result, note: Equals (Object). Method refers to the Equals method in the object class that is not overridden by the quilt class. If two hashcode () return equal results, the Equals method of two objects is not necessarily equal.
Hashcode () method in Java