This article mainly introduces how to share the JAVA-compatible hashCode algorithm code in javascript to achieve the same operation result as that in JAVA. If you need it, you can refer to a hashCode Algorithm in java, it 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:
The 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:
The Code is as follows: