Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: hashCode

Reply
Thread Tools

Re: hashCode

 
 
Lew
Guest
Posts: n/a
 
      08-12-2012
To: =?UTF-8?B?QXJuZSBWYWpow7hq?=
From: Lew <(E-Mail Removed)>

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/wikipedi.../c/cf/Friz.jpg

--- BBBS/Li6 v4.10 Dada-1
* Origin: Prism bbs (1:261/3
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      08-13-2012
To: Lew
From: =?UTF-8?B?QXJuZSBWYWpow7hq?= <(E-Mail Removed)>

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/3
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      08-13-2012
To: Lew
From: =?UTF-8?B?QXJuZSBWYWpow7hq?= <(E-Mail Removed)>

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/3
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 
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