Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

overriding equals to evaluate to true with different class, bad practice ?  RSS feed

 
Cedric Bosch
Ranch Hand
Posts: 99
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm thinking about overriding equals method of some classes so the two instance of different class can be equal together. I feel like this is really prone for confusion, but during my test it performed faster so I don't think that is negligible. (10m entries, 150 ms vs 2000ms)

The reason I'm doing the piece of code under is that then I can avoid object creation (let's say I've a big ass list of person that I'm iterating over) :

instead of creating a new object like this:

I'm just wondering if some people do that, or if this would be utterly retarded and I'm better of with extending HashSet.



I do however feel like this is just asking for future errors....


the test:

 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37256
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cedric,
Yes, this is a bad practice. It is confusing and not the intent of equals(). You could write a custom comparator and pass that to tree set. The comparator can use any logic you want. That's still a hack though. It would be better to have an interface that has getName() or something to show they really have something in common.

Also, think about whether this is really a performance problem in your application. I see your numbers, but not sure if that is a test to exploit this or a typical use case.
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your instincts are correct: this is a bad idea. I'm wondering what problem you are trying to solve. Obviously, a Pear is not a Person. If the two classes have an IS-A relationship, maybe one should be a subclass of the other.
 
Cedric Bosch
Ranch Hand
Posts: 99
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Cedric,
Yes, this is a bad practice. It is confusing and not the intent of equals(). You could write a custom comparator and pass that to tree set. The comparator can use any logic you want.

Also, think about whether this is really a performance problem in your application. I see your numbers, but not sure if that is a test to exploit this or a typical use case.


Thanks that was indeed my feeling about it. And yes it is the test isn't a use case even though I use that (the new object one) on the most frequented page of the web app on about 100 entries but I mean if I can make it faster even so slightly then why not. But I won't do it like that I also felt it was terrible I just wanted a second perspective to be sure.

Thanks
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cedric Bosch wrote:So I'm thinking about overriding equals method of some classes so the two instance of different class can be equal together.

Well that sounds like an odd requirement right there, UNLESS the two classes really do have something in common - for example, an interface or a superclass that is not Object.

There's simply not much point in using two totally random examples like Person and Pear, because they're never actually likely to be compared in real life - and if they are, you'd almost certainly want equals() to return false.

equals() is NOT a simple method. It has several rules that have to be followed, and it's really easy to get wrong; so don't hamstring yourself by trying to force it to compare apples and oranges.

My advice: First come up with an example that makes sense - ie, two classes that you might want to compare - or store in a dataset together - and then ask your question again.

Winston
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37256
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cedric Bosch wrote:T but I mean if I can make it faster even so slightly then why not.

Because the time it takes you to make it faster and the cost in readability aren't worth saving a few milliseconds.

On my computer, you need a million records in the set to see the performance difference you describe. With 100 records in the set, it is a barely measurable difference.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cedric Bosch wrote:but I mean if I can make it faster even so slightly then why not...

Hopefully now you know.

Performance is almost always a really BAD reason (in fact, some would say the worst reason) for doing something in an non-standard way.

Winston
 
Cedric Bosch
Ranch Hand
Posts: 99
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
UNLESS the two classes really do have something in common


Well they do, I just made a simple example because it would be easy to understand; I won't go in the details, I'll just use HashMap as it is more appropriate for what I want to do.

Winston Gutkowski wrote:

Performance is almost always a really BAD reason (in fact, some would say the worst reason) for doing something in an non-standard way.



Thanks for the advice, "Premature optimization is the root of all evil". I find it fun sometimes though to look at it and I learn some doing so.

Thanks guys.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!