• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Interface Comparator

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When a class implements Interface Comparator<T>, it needs to overide all abstract methods of Comparator<T> which are int compare(T o1, T o2) and boolean equals(Object obj)(indicates whether some other object is "equal to" this Comparator).

I am confused now. Then a class will have two equals(). But it doesn't seems to be the case. It seems only int compare(T o1, T o2) needs to be overriden.

Please explain.

Eleanor
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll always have an equals() method inherited from Object,
so if you explicitly implement equals() in a class, then you're overriding it: you won't have two methods.

The Comparator interface will be satisfied fine if you don't override equals(), but if you do, you have to make sure you obey the contract specified in the API:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Comparator.html

See also:

http://www.velocityreviews.com/forums/t145137-comparatorequals.html
 
Eleanor Leong
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much for your information

Eleanor
 
Don't listen to Steve. Just read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic