return hash-code of a string

Discussion in 'Java' started by Flo 'Irian' Schaetz, Aug 11, 2007.

  1. 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();
    Flo 'Irian' Schaetz, Aug 11, 2007
    #1
    1. Advertising

  2. Flo 'Irian' Schaetz

    Eitan M Guest

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

    Thanks :)
    Eitan M, Aug 11, 2007
    #2
    1. Advertising

  3. Flo 'Irian' Schaetz

    Eric Sosman Guest

    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
    lid
    Eric Sosman, Aug 11, 2007
    #3
  4. And thus spoke Eric Sosman...

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


    Correct, my fault.

    Flo
    Flo 'Irian' Schaetz, Aug 11, 2007
    #4
  5. Flo 'Irian' Schaetz

    Lew Guest

    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 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
    Lew, Aug 12, 2007
    #5
  6. Flo 'Irian' Schaetz

    Lew Guest

    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
    Lew, Aug 12, 2007
    #6
  7. Flo 'Irian' Schaetz

    rossum Guest

    On Sun, 12 Aug 2007 11:50:48 +0200, "Eitan M"
    <nospam@nospam_please.com> wrote:

    [Top posting changed]

    >"Flo 'Irian' Schaetz" <> wrote in message
    >news:46be2a32$0$4536$-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


    >
    rossum, Aug 12, 2007
    #7
  8. Flo 'Irian' Schaetz

    Eitan M Guest

    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" <> wrote in message
    news:46be2a32$0$4536$-online.net...
    > And thus spoke Eric Sosman...
    >
    >> Should be hashCode(), with an upper-case C.

    >
    > Correct, my fault.
    >
    > Flo
    Eitan M, Aug 12, 2007
    #8
  9. 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
    Joshua Cranmer, Aug 12, 2007
    #9
    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. Greenhorn
    Replies:
    15
    Views:
    811
    Keith Thompson
    Mar 6, 2005
  2. rp
    Replies:
    1
    Views:
    512
    red floyd
    Nov 10, 2011
  3. Pokkai Dokkai
    Replies:
    8
    Views:
    131
    Phrogz
    Nov 18, 2007
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    608
    David A. Black
    Jul 2, 2008
  5. dt

    convert string to hash of hash?

    dt, Mar 1, 2007, in forum: Perl Misc
    Replies:
    1
    Views:
    464
    ~greg
    Mar 2, 2007
Loading...

Share This Page