Re: do I need to override the equals() method?



Marteno Rodia <marteno_rodia@xxxxx> wrote:
I've defined a class which, of course, silently extends
java.lang.Object, but I've added some my own attributes. Now I want to
be able to compare two objects of my class i.e. to check if they are
identical.

Do I need to write my own equals() method or can I use the
inherited java.lang.Object.equals() method?

The inherited one considers only identity, not content. If you can have two
instances that are completely interchangeable, you should override equals() to
indicate this. And, of course, if you override equals(), you must override
hashCode() to be consistent.

Note that there _IS_ a risk of being too liberal with equality. If two things
compare as equals, then they will replace each other in sets and map keys.
This is often desired, especially if the key fields that determine equality
are final. In the case of mutable objects, though, you often want them _NOT_
to be equal just because they happen to currently be the same. You only want
them equal if they will always be the same, and can literally be interchanged
in a collection without harm.

There are a few patterns to keep this straight:
1) you can define a new method, "equivalent(Object)", that means "currently
the same, but could later change to be different. Then your code can test
for equivalence when you want, but equals() only means "completely
interchangeable for all Collections".

2) you can be careful with using normal collections in some cases, and
Identity collections (IdentityHashMap, for instance) when you need a different
idea of equality. This usually takes more work than #1, and is easier to have
bugs creep in. But it's sometimes necessary if your object model doesn't have
a well-defined permanent equality.

3) create, use, and love immutable objects. Make all fields final, and you
then don't have to worry about two objects that are the same now, but might
later differ.
--
Mark Rafn dagon@xxxxxxxxx <http://www.dagon.net/>
.



Relevant Pages

  • Re: collection framework: using the good interface
    ... The word object's class would implement Comparable, and should have equals and hashCode consistent with compareTo, but I would not make two words equal unless they are, in all respects, the same word. ... I'm more and more convinced that equality should involve all aspects of the Word class, but I'm still uncomfortable with mixing lexicographic information (allowing to compare objects in different collections) and information about frequency, distribution, cooccurrents. ...
    (comp.lang.java.help)
  • RE: Equality vs Sameness
    ... Equals would not offer any benefit. ... >> from it in the future if the users haven't asked for that functionality. ... So you override Equals and GetHashCode even for forms, ... or wanted to compare two instances of a singleton? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: A better cloning mechanism (was: Why not cloneable by default?)
    ... > notion of equality for special cases, much as we do for ordering. ... and students compete in several of them. ... have to not override equals. ... and you know that you only want to compare ...
    (comp.lang.java.programmer)
  • Re: do I need to override the equals() method?
    ... Marteno Rodia a écrit: ... so if you want that equals returne true for "content identical" object, yes you should override the equalsmethod. ... If you put object in collections and use containsfor example, you might want to override equals. ... Make sure they are consistent with each other, i.e., if two instances compare equal, they must have the same hash code. ...
    (comp.lang.java.programmer)
  • Re: Why use Objectg in Equals method?
    ... superclasses (but not subclasses). ... Of course, if you intend to compare with objects of different classes, ... It is convenient that 'equals(Object o)' is an override - it allows for expressive compactness in a ton of algorithms, e.g., those on which collections classes rely. ...
    (comp.lang.java.programmer)