Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > hashcode

Reply
Thread Tools

hashcode

 
 
gk
Guest
Posts: n/a
 
      09-15-2006
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 ?

 
Reply With Quote
 
 
 
 
Hendrik Maryns
Guest
Posts: n/a
 
      09-15-2006
-----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-----
 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      09-15-2006
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
 
Reply With Quote
 
Daniel Dyer
Guest
Posts: n/a
 
      09-15-2006
On Fri, 15 Sep 2006 16:42:08 +0100, Patricia Shanahan <(E-Mail Removed)> 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
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      09-15-2006
Daniel Dyer wrote:
> On Fri, 15 Sep 2006 16:42:08 +0100, Patricia Shanahan <(E-Mail Removed)> 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[i] 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/...ml#hashCode()]

Patricia
 
Reply With Quote
 
Daniel Dyer
Guest
Posts: n/a
 
      09-15-2006
On Fri, 15 Sep 2006 17:51:53 +0100, Patricia Shanahan <(E-Mail Removed)> 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[i] 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/...ml#hashCode()]
>


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

Dan.

--
Daniel Dyer
http://www.dandyer.co.uk
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hashcode of primitive types Dimitri Pissarenko Java 5 01-29-2004 11:05 PM
Improving hashCode() to match equals() Marco Java 10 01-17-2004 09:55 PM
Designing hashCode() methods kelvSYC Java 1 12-24-2003 02:56 AM
equals and hashCode Gregory A. Swarthout Java 2 12-20-2003 12:34 AM
hashCode for byte[] Roedy Green Java 1 08-22-2003 02:08 AM



Advertisments