Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

return hash-code of a string

Thread view: 
Eitan M - 11 Aug 2007 19:32 GMT
Hello.
Is there any hash function in Java, that convert a string value to a hash
code ?

Thanks :)
Flo 'Irian' Schaetz - 11 Aug 2007 19:41 GMT
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();
Eric Sosman - 11 Aug 2007 21:31 GMT
> And thus spoke Eitan M...
>
[quoted text clipped - 3 lines]
> String xyz = "Hello World";
> int hash = xyz.hashcode();

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

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

also works.

Signature

Eric Sosman
esosman@ieee-dot-org.invalid

Flo 'Irian' Schaetz - 11 Aug 2007 22:29 GMT
And thus spoke Eric Sosman...

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

Correct, my fault.

Flo
Eitan M - 12 Aug 2007 10:00 GMT
Hash code returns an integer,
and due that it is limited (it is not CRC),
so a string like : "zzzzz" may have an exception.

> And thus spoke Eric Sosman...
>
[quoted text clipped - 3 lines]
>
> Flo
Lew - 12 Aug 2007 10:21 GMT
> 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" <iryan@gmx.de> 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.

Signature

Lew

Lew - 12 Aug 2007 10:23 GMT
> 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.

Signature

Lew

rossum - 12 Aug 2007 10:37 GMT
[Top posting changed]

>> And thus spoke Eric Sosman...
>>
[quoted text clipped - 5 lines]
>
>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
Joshua Cranmer - 12 Aug 2007 16:34 GMT
> 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.

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.