• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overriding the equlas method

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic