Re: do I need to override the equals() method?
- From: Andreas Leitgeb <avl@xxxxxxxxxxxxxxxxxxxxxxxx>
- Date: 03 Apr 2009 14:25:35 GMT
Albert <albert@xxxxxxxx> wrote:
Marteno Rodia a écrit :
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.
If you want to check for *identity*, then you can just use the inherited
Object.equals() and ignore the rest of this posting.
Do I need to write my own equals() method or can I use theso if you want that equals returne true for "content identical" object,
inherited java.lang.Object.equals() method?
yes you should override the equals(Object) method.
It depends on your uses. If you put object in collections and use
contains() for example, you might want to override equals.
In this case (or really always) if you override .equals() for a less
strict check, you *must* also override .hashCode() to be consistent
with that looser .equals(), otherwise some of the standard collections
may behave wrongly with your items.
.
- References:
- do I need to override the equals() method?
- From: Marteno Rodia
- Re: do I need to override the equals() method?
- From: Albert
- do I need to override the equals() method?
- Prev by Date: Re: Throwing Constructor Exceptions and cleaning up
- Next by Date: Re: java RESTful web service
- Previous by thread: Re: do I need to override the equals() method?
- Next by thread: Re: do I need to override the equals() method?
- Index(es):
Relevant Pages
|