Java的hashCode解析

來源:互聯網
上載者:User

hash表是快速尋找演算法中比較好的一種,雜湊表中的雜湊函數是其中比較重要的。Java使用的Hash函數說明如下

  /**
     * 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;
    } 

由於是Hash函數,所以一般情況下衝突不可避免,比如如下的漢字就會出現相同雜湊值的情況:

Java的Hash值相同的值如下:

古漢語:
        國學寶典字詞頻表(從10億已標點字統計,暫時發布簡體字詞表)1.0[2007-06-10][前1萬個]

國學寶典是目前最大的古籍類全文資料庫,希望以該資料庫為基礎的統計能對搞語言學和檢索引擎的朋友有用
http://www.gxbd.com
http://bbs.guoxue.com[擷取更新]

                  雜湊值
獠 敗走   60774744
羹 弩       -629648662
東京 叉 1835901360

另外現代漢語中也有不少,比如

結算 罰款

hash表的方面還有很多問題,以後再說

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.