equals and hascode

A

Arne Vajhøj

Does it shouldn't be the case?

A good practice is to write immutable object and in case of mutable object,
I agree with you that hashcode could throw a exception to avoid collection
or map problem.
So, in the most of the cases (immutable object), hashcode and equals MAY be
equivalent and hashCode could be computed «statically» in constuctors and my
equals code is good.
In minor cases (immutable object), hashCode + equals mix is a non-sense
concept, which could lead to very very serious problems with all generic
classes (collection, map, set, generic algo…) and MUST be avoided and
disable by an exception.

You may argue that most objects should be immutable, but they
are not.

And there are cases where they should not - and this includes
the domain of the original post.

Arne
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top