Home > Web Front-end > JS Tutorial > body text

Implementation of JAVA-compatible hashCode algorithm code sharing in javascript_Basic knowledge

WBOY
Release: 2016-05-16 16:39:36
Original
1972 people have browsed it

In Java, a hashCode algorithm can be used to calculate the hash value of a string. Today, a friend suddenly asked me if I can calculate hashCode in js. The calculation result is required to be the same as Java's hashCode calculation.

As for Java’s hashCode, I have never understood its algorithm before, but I guess it shouldn’t be too difficult, so now I wrote this code in Java for testing:
Running result: 899755

Press the Ctrl key and click on the hashCode method name to follow up and take a look at the algorithm. I found that it is a very simple code, as shown below:

Copy code 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;
}

Okay now, simply transplant it into js and it should be ok. So I wrote the following JS code:

Copy code The code is as follows:



Running result: 899755

OK, the result is the same as the java calculation. I thought that was done, and then I thought about finding a random string to test:

"Shenyang Shenyang", the running result in JAVA is: 1062711668, but in js it becomes: 26832515444.

I’m so dizzy, there’s something wrong with just trying this! After thinking for a while, I suddenly thought that the length of int in Java seems to be about 2.1 billion, but there is no such limit in js. The problem should be here, so I made a little modification to the previous method:

Copy code The code is as follows:

<script><br> function hashCode(str){<br> var h = 0, off = 0;<br>         var len = str.length;<br> for(var i = 0; i < len; i ){<br />                h = 31 * h str.charCodeAt(off);<br />          }<br /> var t=-2147483648*2;<br /> While(h>2147483647){<br> h =t<br> }<br>           return h;<br> }<br> alert(hashCode('Shenyang Shenyang'));</script>

Test again! OK! You're done. There is no technical content, just a brief summary
Updated on 2013-02-19, the above one is relatively inefficient and will crash when the content is very long. The following code is the optimized code:

Copy code The code is as follows:

<script><br> Function hashCode(str) {<br> var h = 0;<br> var len = str.length;<br> var t = 2147483648;<br> for (var i = 0; i < len; i ) {<br />               h = 31 * h str.charCodeAt(i);<br /> If(h > 2147483647) h %= t;//If java int overflows, take the modulo<br> }<br> /*var t = -2147483648 * 2;<br> ​​​​while (h > 2147483647) {<br>              h = t<br>         }*/<br>          return h;<br> }<br> alert(hashCode('C# How to implement N threads executing concurrently at the same time and the rest in the queue')); //1107373715<br> </script>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!