Re: hashCode

Discussion in 'Java' started by Lew, Aug 13, 2012.

  1. Lew

    Lew Guest

    To: =?UTF-8?B?QXJuZSBWYWpow7hq?=
    From: "Lew" <lew@1:261/38.remove-m2z-this>

    To: =?UTF-8?B?QXJuZSBWYWpow7hq?=
    From: Lew <>

    Arne Vajh-,j wrote:
    > The original questions were:
    >
    > #Is it always technically correct to override the hashCode function #like so:
    > #
    > # @Override
    > # public int hashCode() {
    > # return 1;
    > # }
    >
    > For which the answer is YES. Per documentation.
    >
    > But with really poor performance in many relevant cases.
    >
    > #Would it be potentially better if that was Object's implementation?
    >
    > Which was clarified to:
    >
    > #Better in the sense that you would never HAVE to override hashCode.
    >
    > For which the answer is also YES. Per the previous.


    No, that's not true. Value-equality maps, for example, would not work if you
    didn't override 'hashCode()' in the key type to match value equality on the
    keys.

    > But with the same performance note. And a big sigh because it
    > seems to want to broaden bad performance from a single class
    > to the entire programming style (multiple classes).


    Overriding 'hashCode()' is done for functional reasons, not performance
    reasons. If you fail to override the method, you'll get incorrect behavior, for
    example failing to find a collection member that is actually present.

    --
    Lew
    Honi soit qui mal y pense.
    http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

    -+- BBBS/Li6 v4.10 Dada-1
    + Origin: Prism bbs (1:261/38)
    -+- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
    --- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24
     
    Lew, Aug 13, 2012
    #1
    1. Advertising

  2. To: Lew
    From: "=?UTF-8?B?QXJuZSBWYWpow7hq?=" <=?utf-8?b?qxjuzsbwywpow7hq?=@1:261/38.rem
    ove-nlb-this>

    To: Lew
    From: =?UTF-8?B?QXJuZSBWYWpow7hq?= <>

    On 8/12/2012 12:46 AM, Lew wrote:
    > Arne Vajh-,j wrote:
    >> The original questions were:
    >>
    >> #Is it always technically correct to override the hashCode function
    >> #like so:
    >> #
    >> # @Override
    >> # public int hashCode() {
    >> # return 1;
    >> # }
    >>
    >> For which the answer is YES. Per documentation.
    >>
    >> But with really poor performance in many relevant cases.
    >>
    >> #Would it be potentially better if that was Object's implementation?
    >>
    >> Which was clarified to:
    >>
    >> #Better in the sense that you would never HAVE to override hashCode.
    >>
    >> For which the answer is also YES. Per the previous.

    >
    > No, that's not true. Value-equality maps, for example, would not work if
    > you didn't override 'hashCode()' in the key type to match value equality
    > on the keys.


    That was almost exactly what I answered in my first answer to the original
    poster.

    Then I read it as "would it be better if my class used Object's
    implementation".

    But after reading the clarification then I think it should be read as "would it
    be better if Object used the implementation shown".

    Very different question!

    Arne

    -+- BBBS/Li6 v4.10 Dada-1
    + Origin: Prism bbs (1:261/38)
    -+- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
    --- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24
     
    Arne Vajhøj, Aug 13, 2012
    #2
    1. Advertising

  3. To: Lew
    From: "=?UTF-8?B?QXJuZSBWYWpow7hq?=" <=?utf-8?b?qxjuzsbwywpow7hq?=@1:261/38.rem
    ove-nlb-this>

    To: Lew
    From: =?UTF-8?B?QXJuZSBWYWpow7hq?= <>

    On 8/12/2012 12:46 AM, Lew wrote:
    > Arne Vajh-,j wrote:
    >> But with the same performance note. And a big sigh because it
    >> seems to want to broaden bad performance from a single class
    >> to the entire programming style (multiple classes).

    >
    > Overriding 'hashCode()' is done for functional reasons, not performance
    > reasons. If you fail to override the method, you'll get incorrect
    > behavior, for example failing to find a collection member that is
    > actually present.


    Correct.

    But the return constant is a special case. It functions as it should but
    performs very poorly.

    Arne

    -+- BBBS/Li6 v4.10 Dada-1
    + Origin: Prism bbs (1:261/38)
    -+- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
    --- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24
     
    Arne Vajhøj, Aug 13, 2012
    #3
    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:
    455
    Dale King
    Aug 22, 2003
  2. Marco
    Replies:
    10
    Views:
    791
  3. Gregory A. Swarthout

    equals and hashCode

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

    Designing hashCode() methods

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

    Hashcode of primitive types

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

Share This Page