Java basics: hashCode method, javahashcode
Writer: BYSocket)
Weibo: BYSocket
Douban: BYSocket
I. Preface
The projects of the maserons have recently become dark. I found that I had to give myself some goals about the technical goals:
Focus is important. Focus on Java basics + H5 (learning)
Other operating systems, algorithms, and data structures are used as extracurricular books. Sometimes, that is, the more you focus on the aspect, the worse you are, the worse you are. Bring Java basics today: hashCode Method
Ii. hashCode Method
Hash code (hash code value) is an integer generated by an object. Its generation is irregular. The hash code can be used to obtain information in the object and convert it to the "relatively unique" integer value of the object. All objects have a hash code. hashCode () is a method of the root class Object. The working principle of the hash is not discussed in Java, as long as you know that it is a fast "Dictionary. Here is a picture of foreigners:
3. Two small examples
First, the bricklayer references a section from the Object specification [ipve6 ]:
The general protocol of hashCode is:
- 1. Multiple calls to the same object during Java application executionHashCodeThe same integer must be returned in the same way, provided that the object isEqualsThe information used for comparison is not modified. This integer does not need to be consistent from one execution of an application to another execution of the same application.
- 2. IfEquals (Object)Method, the two objects are equal, then each object of the two objects is called
hashCodeAll methods must generate the same integer result.
- 3. IfEqualsMethod. If the two objects are not equal, callHashCodeThe method does not necessarily generate different integer results. However, programmers should realize that generating different integer results for unequal objects can improve the performance of the hash table.
- Because hashCode is defined in the root class Object, every Object is an Object and has a default hash value, that is, the storage address of the Object. Let's 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:
?
| 1234 |
hashCode1: 147696667 1385112968hashCode2: 147696667 870919696s s1 : truesb sb1: false |
Summary:
1. s and s1 are equal, and hashCode is the same. The second article of [General hashCode Protocol] is verified. The reason is that the hash code of the string is exported from the content. (We will verify the second example)
2. The hashCode method is not defined in StringBuilder. Therefore, the default Object storage address is exported. (Note that there is a native modifier before the hashCode method of the Object. This indicates that the hashCode method is implemented by non-java languages. The specific method is implemented externally and the address of the memory Object is returned .) For more information, see understanding and understanding native.
The bricklayer just mentioned that the character string hash code is exported from the content. Let's take 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;} |
Summary:
1, s [0] * 31 ^ (n-1) + s [1] * 31 ^ (n-2) +... + S [n-1] What is the mathematical formula?
S [I] is the I character of string, and n is the length of String. 31. Why? The following is a reference to objective Java:
31 is selected because it is an odd prime number. If the multiplier is an even number and the multiplication overflows, the information will be lost because multiplication with 2 is equivalent to the shift operation. The advantage of using prime numbers is not obvious, but it is customary to use prime numbers to calculate hash results. 31 has a good feature, that is, using shift and subtraction instead of multiplication can achieve better performance: 31 * I = (I <5)-I. The current VM can automatically complete this optimization.
Iv. Conclusions and suggestions
Indeed, hashCode is a bit obscure, probably because of the mathematical hash function. The following are the conclusions in objective Java:
1. If the object has the same hash code, it is mapped to the same hash bucket. In this way, the hash list degrades and is called a linked list, thus reducing the performance.
2. An Equal object must have an equal hash code.
3. generating unequal hash codes for unequal objects
4. Do not try to remove a key part of an object from hash code calculation to improve performance.
Writer: BYSocket)
Weibo: BYSocket
Douban: BYSocket