请各位大虾帮我解决一个问题,如何讲字符串的hash值计算出来.谢谢了。

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【shuhan1029】截止到2008-07-02 03:07:58的历史汇总数据(不包括此帖):
    发帖的总数量:0                        发帖的总分数:0                        
    结贴的总数量:0                        结贴的总分数:0                        
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:---------------------结分的百分比:---------------------
    无满意结贴率:---------------------无满意结分率:---------------------
    如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
      

  2.   

    String s = "xxxxx";
    int hash = s.hashCode();
      

  3.   

    楼主不用心哦~~在 java.lang.String 的 hashCode 方法的 API DOC 中有算法说明字符串的 hashCode 是如何计算出来的。
      

  4.   

    java.lang.String
    中的源码
    公式:s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]    /**
         * Returns a hash code for this string. The hash code for a
         * <code>String</code> object is computed as
         * <blockquote><pre>
         * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
         * </pre></blockquote>
         * using <code>int</code> arithmetic, where <code>s[i]</code> is the
         * <i>i</i>th character of the string, <code>n</code> is the length of
         * the string, and <code>^</code> indicates exponentiation.
         * (The hash value of the empty string is zero.)
         *
         * @return  a hash code value for this object.
         */
        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;
        }