Java.lnag.Object is the agreement to Hashcode:
During an application execution, if the information used by an object does not have modified information, the HashCode method is called multiple times, it must always return the same integer. If the two objects are equal according to the Equals (Object O) method, the HashCode method to call any object in these two objects must produce the same integer results. If the two objects are not equal according to the Equals (Object O) method, the HashCode method of any of these two objects is called, which does not require different integer results. But if it can be different, the performance of the hash table may be improved.
See not a rewrite hashCode led to the use hashMap not appear expected results example: public final class PhoneNumber {private final short areaCode; private final short exchange; private final short extension; public PhoneNumber (int areaCode, int exchage, int extension) {rangeCheck ( Areacode, 999, "Area Code"); Rangecheck (Exchange, 999, "Exchange"); RangeCheck (Extension, 9999, "Extension"); this.areAcode = (Short) area; excode; this.exchange = (short) Exchange; THIS.EXTENSION = (short) Extension;} Private Static Void Rangecheck (INT ARG, INT MAX, STRING NAME) {IF (Arg <0 || arg> max) throw new illegalgumentException (Name ":" arg);} PUBLIC Boolean Equals (Object O) {IF (o == this) Reutrn true; if (!) Return False; phonenumber pn = (phonenumber) o; return pn.extension == Extension && pn.exchange = Exchange && pn.areracode = aacode;} // no hashcode method ...} There is now the following lines: map m = new hashmap (); M.PUT (New Phonenumber (1, 2, 3), "jenny") What is the return value of M.GET (New Phonenumber (1, 2, 3))? Although this instance is equal, the hash code of two instances is different from the hash code of the two instances (ie, the second request) is not rewritten (ie, the result is NULL instead of "jenny". Ideally A hash function should be evenly distributed to all possible hash values in a collection instance, and below is close to the ideal "prescription":
Save a non-zero constant value (such as 17) in a variable called Result; for each keyword field in the object (referring to each domain considering in the Equals method), complete the following steps:
Calculate the INT type hash code C:
If the domain is a Bloolean type, calculate (f? 0: 1) If the domain is Byte, CHAR, SHORT, or INT type, calculate (int) f If the domain is the LONG type, calculate (int) (f ^ (>>> 32)) If the domain is a float type, calculate float.floatthannelBits (f) If the domain is a Double type, calculate the double.doubitolongbits (f) to get a long type value, then calculate this long type as described above. Hatigated value If the domain is an object reference, use this object's HashCode, if the value of the domain is null, return 0 If the domain is an array, each array element is treated as a separate domain, The next step is then combined with the following formula to combine the hash code C into the Result. Result = 37 * Result C; Check "Is the equal instance of equal hash code?", if it is not, correct the error. According to this prescription, you have a Hashcode method for Phonenumber:
Public Int hashcode () {
Int result = 17;
Result = 37 * Result areacode;
Result = 37 * Result exchange;
Result = 37 * Result extension;
Return Result;
}
If the cost of calculating the restriction code is relatively high, you can consider saving this code inside, generating it is generated or sluggish initialization. Do not try to exclude a key portion of an object from the hash code calculation to improve performance.