Javascript implements JAVA-compatible hashCode algorithm code sharing and javascripthashcode
A hashCode Algorithm in java can be used to calculate the hash value of a string. Today, a friend suddenly asked me if he could calculate the hashCode in js, which requires the same calculation result as that in java.
I have never understood java's hashCode algorithm before, but I guess it should not be too difficult. So now I have written this code in java for testing:
Result: 899755
Press Ctrl and click the hashCode method name to check its algorithm. The Code is as follows:
Copy codeThe Code is as follows:
Public int hashCode (){
Int h = hash;
If (h = 0 ){
Int off = offset;
Char val [] = value;
Int len = count;
For (int I = 0; I <len; I ++ ){
H = 31 * h + val [off ++];
}
Hash = h;
}
Return h;
}
This is good. It should be okay to simply port it to js. Write the following JS Code:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function hashCode (str ){
Var h = 0, off = 0;
Var len = str. length;
For (var I = 0; I <len; I ++ ){
H = 31 * h + str. charCodeAt (off ++ );
}
Return h;
}
Alert (hashCode ('shenyang '));
</Script>
Result: 899755
OK, same as the java computing result. I thought this was done, and then I thought about finding a string for testing:
"Shenyang", the result of running in JAVA is: 1062711668, but it became: 26832515444 in js.
This is a problem if you try it! After thinking for a moment, I suddenly thought that the int length in Java seems to be about 2.1 billion, and this limit is not applied in js. The problem should be here, so I made some changes to the previous method:
Copy codeThe Code is as follows:
<Script>
Function hashCode (str ){
Var h = 0, off = 0;
Var len = str. length;
For (var I = 0; I <len; I ++ ){
H = 31 * h + str. charCodeAt (off ++ );
}
Var t =-2147483648*2;
When (h> 2147483647 ){
H + = t
}
Return h;
}
Alert (hashCode ('shenyang Shenyang Ah'); </script>
Test again! OK! Success. No technical content.
Updated on February 19,. the above Code is inefficient and will be taken off when the content is long. The following code is the optimized code:
Copy codeThe Code is as follows:
<Script>
Function hashCode (str ){
Var h = 0;
Var len = str. length;
Var t = 2147483648;
For (var I = 0; I <len; I ++ ){
H = 31 * h + str. charCodeAt (I );
If (h> 2147483647) h % = t; // if java int overflows, the modulo is obtained.
}
/* Var t =-2147483648*2;
When (h> 2147483647 ){
H + = t
}*/
Return h;
}
Alert (hashCode ('C # How do N threads execute concurrently at the same time, while others implement in the queue '); // 1107373715
</Script>
A question about hashcode in java
Hi, this is the problem!
Hash code is an integer value exported from an object. Theoretically, it is irregular. Different objects should have different hashcodes.
The algorithm for retrieving hash codes from the String class in the standard String library is as follows:
Int hash = 0;
For (int I = 0; I <length (); I ++) hash = 31 * hash + charAt (I );
It can be seen that the hash codes of the String type s1 and s2 are exported Based on the content, because they have the same content, so the hash codes are the same.
For details, please refer to the java Library source code or java technology core technology volume 1 5.2.3 and wish you some help
HashCode in Java
1. May different strings have the same HashCode?
Yes!
2. May the same string have different HashCode?
If you rewrite the equals and hashcode methods by yourself, but the java specification requires that the two equals objects must have the same hashcode.
3. Are the hashcodes generated by the same strings the same?
Your implementation can be different, but the java specification requires that the hashcode generated each time for the same object must be the same!
For details, refer to the equals method and hashCode method in the Object of the API.