D
Danno
Just curious
Danno said:Just curious
Manish said:I use equals rarely, when I have to compare DTOs based on a set of
attributes - for example, comparing 2 persons based on their firstNames
being same, ignoring other attributes.
hashCode - never.
Arne said:You know what you should do ...
Manish said:
Danno said:Just curious
Danno said:Just curious
Patricia said:Definitely not. I implement toString when there is a short, but
interesting, String representation. I stay with the Object equals and
hashCode unless the class has a strong natural concept of value
equality, which generally involves immutability.
Patricia
Just curious
Chris said:[In general it is a bad idea to put the question into the subject line without
repeating it in the main body of the text].
Just curious
No, certainly not. That would be a very bad mistake.
It's always worth /considering/ overriding toString(), but rarely actually
worthwhile. It would often be an /error/ to override equals()/hashCode().
-- chris
You'll know when you need equals()... most things
don't get compared, so there's usually no reason to
bother.
Once you've put in your own equals() method, you really
need to override hashCode(), too. But pre-mature optimization
is evil, so here's the one I always use pre-profiling:
public int hashCode() {
return 0;
}
Mark said:Once you've put in your own equals() method, you really
need to override hashCode(), too. But pre-mature optimization
is evil, so here's the one I always use pre-profiling:
public int hashCode() {
return 0;
}
I'm not kidding.
Chris said:You should be.
/Anything/ would be better than that. Throw a runtime exception -- at least
that would guarantee that you weren't depending on having a working hashCode()
without realising it.
I assume that if Mark found poor hash table performance during
profiling, he would upgrade the hashCode() method for the key class.
That said, when I'm writing an equals(), I know exactly which features
must be equal for two instances to be considered equal, which is the
only difficult part of writing a good hashCode(). I just write the two
methods together.
Matt said:My question is, what is a good first implementation of compound hashCode?
For objects that have simple fields a, b, c I tend to use
a.hashCode ^ b.hashCode ^ c.hashCode
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.