• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overriding the equlas method  RSS feed

 
Dounia Kchiere
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I would like to know how I can override the equals method( to compare two objects) in a class that implements a Comparator..

Thank you so much for your help,

Dounia
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What specifically is giving you a problem? Is this an interview or homework question?

Please, nobody just give the very obvious answer.
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Sherriff:

You are in a beginner's forum. Maybe you should go to the intermediate or advanced forum.

I think that this is the answer to the question:

public boolean equals(Object other) {
return compare(this, other);
}

I'm assuming that the compare method of the comparator interface is already implemented.

-- Kaydell
[ November 25, 2006: Message edited by: Kaydell Leavitt ]
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't do that. The compare method will likely call equals (or at least can), causing an infinite loop

And remember to always override hashCode() when you override equals(), one without the other is asking for trouble.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!