Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > return hash-code of a string

Reply
Thread Tools

return hash-code of a string

 
 
Flo 'Irian' Schaetz
Guest
Posts: n/a
 
      08-11-2007
And thus spoke Eitan M...

> Is there any hash function in Java, that convert a string value to a hash
> code ?


String xyz = "Hello World";
int hash = xyz.hashcode();
 
Reply With Quote
 
 
 
 
Eitan M
Guest
Posts: n/a
 
      08-11-2007
Hello.
Is there any hash function in Java, that convert a string value to a hash
code ?

Thanks


 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      08-11-2007
Flo 'Irian' Schaetz wrote:
> And thus spoke Eitan M...
>
>> Is there any hash function in Java, that convert a string value to a hash
>> code ?

>
> String xyz = "Hello World";
> int hash = xyz.hashcode();


Should be hashCode(), with an upper-case C.

int hash = "Hello World".hashCode();

also works.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Flo 'Irian' Schaetz
Guest
Posts: n/a
 
      08-11-2007
And thus spoke Eric Sosman...

> Should be hashCode(), with an upper-case C.


Correct, my fault.

Flo
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      08-12-2007
Eitan M wrote:
> Hash code returns an integer,


Please do not top-post (placement of your reply above the material to which
you're replying).

> and due that it is limited (it is not CRC),


What is the limitation that concerns you, and how does it cause trouble?

Why is CRC needed?

> so a string like : "zzzzz" may have an exception.


What sort of exception do you mean?

"Flo 'Irian' Schaetz" <(E-Mail Removed)> wrote in message
And thus spoke Eric Sosman...
>>> hashCode()


By definition, a hash reduces information from a source (document, byte
sequence, ...). That is, in fact, the point of a hash code in the first
place. The reduced information set is easier to manipulate than the source
domain set, and by dint of being int, faster, too. Not to put to fine a point
on it, hash codes are a hack to compensate for human impatience.

This aside from their cryptographic utility, but that's not essential to
Object.hashCode().

So if you want a hash code, you want to reduce information. There's no point
in asking for a hash code that doesn't reduce information.

--
Lew
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      08-12-2007
Eitan M wrote:
> Hash code returns an integer,


Please do not top-post (placement of your reply above the material to which
you're replying).

> and due that it is limited (it is not CRC),


What is the limitation that concerns you, and how does it cause trouble?

Why is CRC needed?

> so a string like : "zzzzz" may have an exception.


What sort of exception do you mean?

"Flo 'Irian' Schaetz" wrote
And thus spoke Eric Sosman...
>> hashCode()


By definition, a hash reduces information from a source (document, byte
sequence, ...). That is, in fact, the point of a hash code in the first
place. The reduced information set is easier to manipulate than the source
domain set, and by dint of being int, faster, too. Not to put too fine a
point on it, hash codes are a hack to compensate for human impatience.

This aside from their cryptographic utility, but that's not essential to
Object.hashCode().

So if you want a hash code, you want to reduce information. There's no point
in asking for a hash code that doesn't reduce information.

--
Lew
 
Reply With Quote
 
rossum
Guest
Posts: n/a
 
      08-12-2007
On Sun, 12 Aug 2007 11:50:48 +0200, "Eitan M"
<nospam@nospam_please.com> wrote:

[Top posting changed]

>"Flo 'Irian' Schaetz" <(E-Mail Removed)> wrote in message
>news:46be2a32$0$4536$(E-Mail Removed)-online.net...
>> And thus spoke Eric Sosman...
>>
>>> Should be hashCode(), with an upper-case C.

>>
>> Correct, my fault.
>>
>> Flo

>
>Hash code returns an integer,

Of course hashCode() returns an integer, that is what it does. If you
do not want an integer then do not use hashCode().

>and due that it is limited (it is not CRC),

If you want a CRC then use a CRC.

>so a string like : "zzzzz" may have an exception.

I am not clear what you mean here. On my system

"zzzzz".hashCode() returns 116425210

rossum


>


 
Reply With Quote
 
Eitan M
Guest
Posts: n/a
 
      08-12-2007
Hash code returns an integer,
and due that it is limited (it is not CRC),
so a string like : "zzzzz" may have an exception.

"Flo 'Irian' Schaetz" <(E-Mail Removed)> wrote in message
news:46be2a32$0$4536$(E-Mail Removed)-online.net...
> And thus spoke Eric Sosman...
>
>> Should be hashCode(), with an upper-case C.

>
> Correct, my fault.
>
> Flo



 
Reply With Quote
 
Joshua Cranmer
Guest
Posts: n/a
 
      08-12-2007
Eitan M wrote:
> Hash code returns an integer,
> and due that it is limited (it is not CRC),
> so a string like : "zzzzz" may have an exception.
>


If I understand you correctly, what you are trying to say is that for
very long Strings, hashCode will fail with an exception. That is not
true: hashCode will only fail on a null object (for different reasons);
and the hash code will always be integral but not necessarily positive
(which is true for all objects). If you want a guaranteed positive hash
code, then use Math.abs(obj.hashCode()).

In short: hashCode will always return (except for null objects) an
integral value for any Java built-in object (negating bugs) and should
do so for any Java object.

I don't understand what you mean by "limited", though.


--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
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
Javadoc including "java.lang.String" instead of just "String" for return values Randolf Richardson Java 2 02-03-2007 07:14 AM
Search a Text File for a String, Return String to Function cl@supportreport.org Perl Misc 1 07-29-2006 10:14 PM
Carriage Return added during return of large string from class method Xeno Campanoli Ruby 0 02-13-2006 08:39 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Web Services return string: An XML string DotNetGruven ASP .Net Web Services 3 06-30-2004 06:48 PM



Advertisments