javascript 实现 java 中String的hashcode方法

13 篇文章 0 订阅

转自:http://zhhphappy.iteye.com/blog/1124283

javascript中有需要用到像类似java中的hashcode方法,想把java中的hashcode方法直接搬到js中发现不行

Java代码   收藏代码
  1. /** 
  2.      * Returns a hash code for this string. The hash code for a 
  3.      * <code>String</code> object is computed as 
  4.      * <blockquote><pre> 
  5.      * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] 
  6.      * </pre></blockquote> 
  7.      * using <code>int</code> arithmetic, where <code>s[i]</code> is the 
  8.      * <i>i</i>th character of the string, <code>n</code> is the length of 
  9.      * the string, and <code>^</code> indicates exponentiation. 
  10.      * (The hash value of the empty string is zero.) 
  11.      * 
  12.      * @return  a hash code value for this object. 
  13.      */  
  14.     public int hashCode() {  
  15.     int h = hash;  
  16.         int len = count;  
  17.     if (h == 0 && len > 0) {  
  18.         int off = offset;  
  19.         char val[] = value;  
  20.   
  21.             for (int i = 0; i < len; i++) {  
  22.                 h = 31*h + val[off++];  
  23.             }  
  24.             hash = h;  
  25.         }  
  26.         return h;  
  27.     }  

 关键是hash的类型是int,int的范围是MIN_VALUE = 0x80000000; MAX_VALUE = 0x7fffffff;

这样在执行

Java代码   收藏代码
  1. h = 31*h + val[off++];  

 的时候当超有可能超过int的最大值,变成负数。但js里面是不区分int,long的直接显示,显示不下用科学技术法,所以需要一个模拟java中int超出范围变负数的方法。

最终的实现如下:

Js代码   收藏代码
  1. function isNull(str){  
  2.         return str == null || str.value == "";  
  3.     }  
  4.       
  5.     /** 
  6.      * java String hashCode 的实现 
  7.      * @param strKey 
  8.      * @return intValue 
  9.      */  
  10.     function hashCode(strKey)  
  11.     {  
  12.         var hash = 0;  
  13.         if(!isNull(strKey))  
  14.         {  
  15.             for (var i = 0; i < strKey.length; i++)  
  16.             {  
  17.                 hash = hash * 31 + strKey.charCodeAt(i);  
  18.                 hash = intValue(hash);  
  19.             }  
  20.         }  
  21.         return hash;  
  22.     }  
  23.   
  24.     /** 
  25.      * 将js页面的number类型转换为java的int类型 
  26.      * @param num 
  27.      * @return intValue 
  28.      */  
  29.     function intValue(num)  
  30.     {  
  31.         var MAX_VALUE = 0x7fffffff;  
  32.         var MIN_VALUE = -0x80000000;  
  33.         if(num > MAX_VALUE || num < MIN_VALUE)  
  34.         {  
  35.             return num &= 0xFFFFFFFF;  
  36.         }  
  37.         return num;  
  38.     }  
  39. /**
     * 得到分片号
     * @param strKey
     * @return intValue
     */
    function getShard(strKey){
        var shardNum=8;
        var index=hashCode(strKey)%shardNum;
        if(index<0)
            index+=shardNum;
        return index;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值