Writer:bysocket (mud and brick pulp carpenter)
Weibo: Bysocket
Watercress: Bysocket
First, preface
Masons were recently weeping by the project. Find some to give yourself some goals, about the technical goals:
Focus is important. Focus on Java Basics + H5 (Learn)
Other operating systems, algorithms, data structures as extracurricular Book Expo. Sometimes, the more you focus on yourself, the more you hit the college and the worse. Today brings the Java Foundation: Hashcode method
Second, Hashcode method
hash code, or hash code value, is an integer value produced by an object. There is no regularity in its generation. The hash code can get the information in the object and turn it into the "relative unique" integer value of that object. All objects have a hash code, and Hashcode () is a method of the root class object. The hash list works on the Java basis, as long as you know it is a fast "dictionary". The following refers to the foreigner a picture:
Three, two small examples
First the Masons cite a section from the object specification "JavaSE6":
The General agreement of Hashcode is:
- 1. During Java application execution, when the hashcode method is called multiple times on the same object, the same integer must be returned consistently, provided that the information used to compare the object to equals is not modified. The integer does not need to be consistent from one execution of an application to another execution of the same application.
- 2. If two objects are equal according to the equals (object) method, the method that is called for each object in both objects
hashCode must produce the same integer result.
- 3. If according to equals method, two objects are not equal, then calling the hashcode method on any of these two objects does not require that a different integer result be generated. However, programmers should be aware that generating different integer results for unequal objects can improve the performance of the hash table.
- Since hashcode is defined in the root class object, each object is an object and has a default hash value, which is the storage address of the object. Mason, please take a look at this example:
?
| 1234567891011121314151617 |
public class HashCodeTest{ public static void main(String[] args) { String s = "hashCode"; StringBuilder sb = new StringBuilder(s); System.out.println("hashCode1: " + s.hashCode() + " " + sb.hashCode()); String s1 = new String("hashCode"); StringBuilder sb1 = new StringBuilder(s1); System.out.println("hashCode2: " + s1.hashCode() + " " + sb1.hashCode()); // are they equals? System.out.println("s s1 : " + s.equals(s1)); System.out.println("sb sb1: " + sb.equals(sb1)); }} |
Run a bit and you can see it on the console:
?
| 1234 |
hashCode1: 147696667 1385112968hashCode2: 147696667 870919696s s1 : truesb sb1: false |
Mason Summary :
1, S and s1 equal, and hashcode the same. The second article of "Hashcode General Agreement " was validated. The reason is that the hash code of the string is derived from the content. (This second example we will verify)
2, StringBuilder There is no definition of hashcode method, so the object is exported to the default address stored on objects. (Note that the Hashcode method of object has a native modifier in front of it, which means that the Hashcode method is implemented by a non-Java language, and the specific method is implemented externally, returning the address of the memory object.) For more information, see Understanding & Understanding Keywords Native actual combat chapter.
The Masons just mentioned that the string hash code is derived from the content. Here's a look at the implementation of string hashcode.
?
| 12345678910111213141516171819202122232425 |
/** The value is used for character storage */private char value[];private int hash;// Default to 0/** * Returns a hash code for this string. The hash code for a * String object is computed as * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] */public int hashCode(){ int h = hash; if (h == 0 && value.length > 0) { char val[] = value; for (int i = 0; i < value.length; i++) { h = 31 * h + val[i]; } hash = h; } return h;} |
Mason Summary :
1, s[0]*31^ (n-1) + s[1]*31^ (n-2) + ... + s[n-1] What does the mathematical formula represent?
S[i] is the I character of a string, and n is the length of a string. 31 Why? The following is a quote from the words "effective Java":
31 is chosen because it is an odd prime, and if the multiplier is even, and the multiplication overflows, the information is lost because multiplying with 2 is equivalent to the shift operation. The benefits of using prime numbers are not obvious, but it is customary to use prime numbers to calculate hash results. 31 There is a good feature, that is, using shift and subtraction instead of multiplication, you can get better performance: 31*i== (i<<5)-I. Today's VMs can automatically do this optimization.
Iv. Conclusions and advice
It is true that hashcode is a bit obscure, possibly because of that mathematical hash function. Here is the conclusion point in effective Java:
1, if the object has the same hash code, is mapped to the same hash bucket, so that the hash list degradation is called linked list, so performance is reduced.
2. Objects of equal size must have equal hash codes
3. Generate unequal hash codes for unequal objects
4. Do not attempt to eliminate a key part of an object from the hash code calculation to improve performance
Writer:bysocket (mud and brick pulp carpenter)
Weibo: Bysocket
Watercress: Bysocket
Java Basics: Hashcode method