From string source to uncover Hashcode () algorithm __ algorithm

Source: Internet
Author: User

First look at the Java source in the Hashcode () method of the variables used in the declaration.

/** The value is used for character storage. * *
    Private Final char value[];//defines a character array value, which is used to store the characters inside the string/** the offset is the ' the ' of the ' the '

    storage that I S used. *
    Private Final int offset;//defines an offset variable to represent the index of the first character of the string/** the count is the number of characters in the string

    . * /
    Private Final int count;//define the count variable to represent the number of characters in the string

    /** Cache The hash code for the string *
    /private int hash; /default to 0, defaults to 0

The following is the source code of the Hashcode () method

 /**      * 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]      * &LT;/PR e></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 t
He length of      * the string, and <code>^</code> indicates exponentiation.      * (the hash value of the empty string is zero.)      *      * @return &N
Bsp;a hash code value for this object.      */    public int hashcode () {        int h = hash;     &NBSP ;   if (h = 0 && count > 0) {    &nbsp
      int off = offset;
            char val[] = value;


            int len = count;             for (int i = 0; i < len; i++) {            &NB Sp
  h = 31*h + val[off++];
                        hash = h;
       }         return h;
    }

Let's take cat for example. According to the hashcode () source of the loop part of the algorithm, calculate the hashcode value of cat

First, the ASCII code value of the C,a,t is 99, 97,116, and the number of characters is 3, so the code for the Loop section changes as follows

public int hashcode () {
        int h = hash;
        if (h = = 0 && count > 0) {
            int off = 0;
            Char val[] = value;
            int len = 3;

            for (int i = 0; i < 3; i++) {
                h = 31*h + val[off++];
            }
            hash = h;
        }
        return h;
    }

Which is a total of 3 cycles.

First cycle: h1=31*0+val[0]=val[0]= ' C ' =99

Second cycle: h2=31*h1+val[1]=31*99+ ' a ' =31*99+97=3166

Third cycle: h3=31*h2+val[2]=31*3166+ ' t ' =31*3166+116=98262

So the hashcode value of cat is 98262.

Then use the expression of polynomial to check s[0]*31^ (n-1) + s[1]*31^ (n-2) + ... + s[n-1]

99*31 squared +97*31+116 result is equal to 98262.

So the calculation is correct.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.