Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface Comparator

 
Eleanor Leong
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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
 
Matt Russell
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks very much for your information

Eleanor
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic