hashcode

Discussion in 'Java' started by gk, Sep 15, 2006.

  1. gk

    gk Guest

    public int hashCode()

    Returns a hash code for this string. The hash code for a String
    object is computed as

    s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]



    How this value is calculated ?

    say i have String s="abc"

    so ,

    s[0]= a
    s[1]=b
    s[2]=c

    but how do i get the power of a character ?

    say the first term ...
    a*31^2 = whats this ? whats the neumerial value of it ?
     
    gk, Sep 15, 2006
    #1
    1. Advertising

  2. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    gk schreef:
    > public int hashCode()
    >
    > Returns a hash code for this string. The hash code for a String
    > object is computed as
    >
    > s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
    >
    >
    >
    > How this value is calculated ?
    >
    > say i have String s="abc"
    >
    > so ,
    >
    > s[0]= a
    > s[1]=b
    > s[2]=c
    >
    > but how do i get the power of a character ?
    >
    > say the first term ...
    > a*31^2 = whats this ? whats the neumerial value of it ?


    chars are converted to ints if arithmetic is done with them.

    H.
    - --
    Hendrik Maryns
    http://tcl.sfs.uni-tuebingen.de/~hendrik/
    ==================
    http://aouw.org
    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (GNU/Linux)

    iD8DBQFFCr6Te+7xMGD3itQRAsa1AJsFRAnoc7o/6FjxpFd9FqWWVVeI8ACePsom
    i2NL19s7Sbar2yLAW51GA/M=
    =/Thm
    -----END PGP SIGNATURE-----
     
    Hendrik Maryns, Sep 15, 2006
    #2
    1. Advertising

  3. Hendrik Maryns wrote:
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > gk schreef:
    >> public int hashCode()
    >>
    >> Returns a hash code for this string. The hash code for a String
    >> object is computed as
    >>
    >> s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
    >>
    >>
    >>
    >> How this value is calculated ?
    >>
    >> say i have String s="abc"
    >>
    >> so ,
    >>
    >> s[0]= a
    >> s[1]=b
    >> s[2]=c
    >>
    >> but how do i get the power of a character ?


    The calculation does not call for any powers of characters, only powers
    of 31. Exponentiation, which is what is meant in this context by "^",
    has higher precedence than multiplication.

    >>
    >> say the first term ...
    >> a*31^2 = whats this ? whats the neumerial value of it ?

    >
    > chars are converted to ints if arithmetic is done with them.


    Also, it is not normal, or efficient, to evaluate a polynomial by
    explicitly calculating e.g. x^(n-1) etc.

    Usually, it is done by alternately adding a coefficient, starting with
    the one for the highest power, and then multiplying by x (in this case,
    by 31).

    So start with s[0]. While the last character to have been used is not
    the last character of the string, multiply the sum so far by 31 and add
    the next character.

    Patricia
     
    Patricia Shanahan, Sep 15, 2006
    #3
  4. gk

    Daniel Dyer Guest

    On Fri, 15 Sep 2006 16:42:08 +0100, Patricia Shanahan <> wrote:

    > Hendrik Maryns wrote:
    >> -----BEGIN PGP SIGNED MESSAGE-----
    >> Hash: SHA1
    >> gk schreef:
    >>> public int hashCode()
    >>>
    >>> Returns a hash code for this string. The hash code for a String
    >>> object is computed as
    >>>
    >>> s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
    >>>
    >>>
    >>>
    >>> How this value is calculated ?
    >>>
    >>> say i have String s="abc"
    >>>
    >>> so ,
    >>>
    >>> s[0]= a
    >>> s[1]=b
    >>> s[2]=c
    >>>
    >>> but how do i get the power of a character ?

    >
    > The calculation does not call for any powers of characters, only powers
    > of 31. Exponentiation, which is what is meant in this context by "^",
    > has higher precedence than multiplication.
    >


    This isn't Pascal, so that's XOR, which has a lower precedence than
    multiplication.

    Dan.

    --
    Daniel Dyer
    http://www.dandyer.co.uk
     
    Daniel Dyer, Sep 15, 2006
    #4
  5. Daniel Dyer wrote:
    > On Fri, 15 Sep 2006 16:42:08 +0100, Patricia Shanahan <> wrote:
    >
    >> Hendrik Maryns wrote:
    >>> -----BEGIN PGP SIGNED MESSAGE-----
    >>> Hash: SHA1
    >>> gk schreef:
    >>>> public int hashCode()
    >>>>
    >>>> Returns a hash code for this string. The hash code for a String
    >>>> object is computed as
    >>>>
    >>>> s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
    >>>>
    >>>>
    >>>>
    >>>> How this value is calculated ?
    >>>>
    >>>> say i have String s="abc"
    >>>>
    >>>> so ,
    >>>>
    >>>> s[0]= a
    >>>> s[1]=b
    >>>> s[2]=c
    >>>>
    >>>> but how do i get the power of a character ?

    >>
    >> The calculation does not call for any powers of characters, only powers
    >> of 31. Exponentiation, which is what is meant in this context by "^",
    >> has higher precedence than multiplication.
    >>

    >
    > This isn't Pascal, so that's XOR, which has a lower precedence than
    > multiplication.


    You seem to have been confused by missing context. The quote is neither
    Pascal, nor Java, nor any other programming language, but a mathematical
    expression represented as text in the String hashCode documentation. In
    context:

    "Returns a hash code for this string. The hash code for a String object
    is computed as

    s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

    using int arithmetic, where s is the ith character of the string, n
    is the length of the string, and ^ indicates exponentiation. (The hash
    value of the empty string is zero.)"

    [http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#hashCode()]

    Patricia
     
    Patricia Shanahan, Sep 15, 2006
    #5
  6. gk

    Daniel Dyer Guest

    On Fri, 15 Sep 2006 17:51:53 +0100, Patricia Shanahan <> wrote:

    > Daniel Dyer wrote:
    >>> The calculation does not call for any powers of characters, only powers
    >>> of 31. Exponentiation, which is what is meant in this context by "^",
    >>> has higher precedence than multiplication.
    >>>

    >> This isn't Pascal, so that's XOR, which has a lower precedence than
    >> multiplication.

    >
    > You seem to have been confused by missing context. The quote is neither
    > Pascal, nor Java, nor any other programming language, but a mathematical
    > expression represented as text in the String hashCode documentation. In
    > context:
    >
    > "Returns a hash code for this string. The hash code for a String object
    > is computed as
    >
    > s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
    >
    > using int arithmetic, where s is the ith character of the string, n
    > is the length of the string, and ^ indicates exponentiation. (The hash
    > value of the empty string is zero.)"
    >
    > [http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#hashCode()]
    >


    Oops, sorry. I've never thought to look at how String implements hashCode.

    Dan.

    --
    Daniel Dyer
    http://www.dandyer.co.uk
     
    Daniel Dyer, Sep 15, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green

    hashCode for byte[]

    Roedy Green, Aug 22, 2003, in forum: Java
    Replies:
    1
    Views:
    424
    Dale King
    Aug 22, 2003
  2. Marco
    Replies:
    10
    Views:
    771
  3. Gregory A. Swarthout

    equals and hashCode

    Gregory A. Swarthout, Dec 19, 2003, in forum: Java
    Replies:
    2
    Views:
    356
    Silvio Bierman
    Dec 20, 2003
  4. kelvSYC

    Designing hashCode() methods

    kelvSYC, Dec 23, 2003, in forum: Java
    Replies:
    1
    Views:
    378
    Ulrich Stern
    Dec 24, 2003
  5. Dimitri Pissarenko

    Hashcode of primitive types

    Dimitri Pissarenko, Jan 29, 2004, in forum: Java
    Replies:
    5
    Views:
    5,859
    Hylander
    Jan 29, 2004
Loading...

Share This Page