Hashcode is a method that has been defined in object, called a hash code, to see how object describes it.
/*** Returns A hash code value for the object. This method was * supported for the benefit of hash tables such as those provided by * {@linkJava.util.HashMap}. * <p> * The general contract of {@codeHashcode} is: * <ul> * <li>whenever It's invoked on the same object more than once during * An execution of a Java application, the {@codeHashcode} method * must consistently return the same integer, provided no information * used in {@codeequals} Comparisons on the object is modified. * This integer need not remain consistent from one execution of a * application to another execution of the S AME application. * <li>if Objects is equal according to the {@codeequals (Object)} * method, then calling the {@codeHashcode} method on each of * the objects must produce the same integer result. * <li>it is <em>not</em> required so if the objects is unequal * According to the {@linkjava.lang.object#equals (Java.lang.Object)} * method, then calling the {@codeHashcode} method on each of the * objects must produce distinct integer results. However, the * programmer should be aware this producing distinct integer results * for unequal objects Ma Y improve the performance of hash tables. * </ul> * <p> * As much as is reasonably practical, the Hashcode method defined by * class {@codeObject} does return distinct integers for distinct * objects. (This was typically implemented by converting the internal * address of the object to an integer, but this Implementa tion * technique isn't required by the * java™ Programming language.) * * @returna hash code value for the This object. * @seejava.lang.object#equals (java.lang.Object) *@seeJava.lang.system#identityhashcode*/ Public native intHashcode ();
Hashcode if overrides are not overridden, the default is to store the address of an object that is exported by object. The main application scenarios are HashMap and hashset and so on hash collection class inside
Example 1?? : The Hashcode () method of String
Public Static void Main (string[] args) { string A1=new string ("Zhang"); String A2=new string ("Zhang"); String A3=new string ("WU"); HashSet<String> h1=new hashset<>(); H1.add (A1); H1.add (A2); H1.add (A3); SYSTEM.OUT.PRINTLN (H1);}
Output Result:
I added three objects in, why HashSet only two elements, this is because string covers the Hashcode () method of object, let's take a look at String hashcode ()
/*** Returns A hash code for this string. The hash code for a * {@codeString} object is computed as * <blockquote><pre> * s[0]*31^ (n-1) + s[1]*31^ (n-2) + ... + s[n-1] * </pre></blockquote> * Using {@codeint} arithmetic, where {@codeS[i]} is the * <i>i</i>th character of the string, {@coden} is the length of the string, and {@code^} indicates exponentiation. * (The hash value of the empty string is zero.) * * @returna hash code value for the This object. */ Public inthashcode () {inth =Hash; if(h = = 0 && value.length > 0) { CharVal[] =value; for(inti = 0; i < value.length; i++) {h= * H +Val[i]; } Hash=h; } returnh; }
The hashcode value of the string is added to the chat value for each character. HashSet Although three objects were added, the values of two objects are the same, so HashSet only holds two elements.
Example 2?? : The Hashcode () method of Object
We will instance 1?? Type the string in the object type with the following code:
Public Static void Main (string[] args) { object a1=new Object (); Object A2=new object (); Object a3=New object (); HashSet<Object> h1=new hashset<>(); H1.add (A1); H1.add (A2); H1.add (A3); SYSTEM.OUT.PRINTLN (H1);}
Output Result:
Ok,hashset saved three objects, no problem, because the object's hashcode default is the storage address
Example 3?? Custom class override Hashcode () method
Here I define a class with the following code:
Public classHashcodeobject {Private intA; Public intGeta () {returnA; } Public voidSetA (inta) { This. A =A; } @Override Public Booleanequals (Object obj) {if(objinstanceofhashcodeobject) { return This. a==((hashcodeobject) obj). Geta (); } return false; } @Override Public inthashcode () {return This. A; }}
Then, the instance 2?? object into our own defined class, as follows
Public Static void Main (string[] args) { hashcodeobject a1=new hashcodeobject (); Hashcodeobject a2=new hashcodeobject (); Hashcodeobject a3=New hashcodeobject (); HashSet<HashCodeObject> h1=new hashset<>(); H1.add (A1); H1.add (A2); H1.add (A3); SYSTEM.OUT.PRINTLN (H1);}
Output Result:
Note that overwriting the Hashcode () method also overrides the equal () method, otherwise the effect is not reached, and the equal () and hashcode () definitions must be consistent if equal () returns True, then hashcode should
Have the same value.
Talk about Java's hashcode usage scenarios