File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interface Comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interface Comparator" Watch "Interface Comparator" New topic

Interface Comparator

Eleanor Leong

Joined: Mar 24, 2006
Posts: 21
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.

Matt Russell
Ranch Hand

Joined: Aug 15, 2006
Posts: 165
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:

See also:

Inquisition: open-source mock exam simulator for SCJP and SCWCD
Eleanor Leong

Joined: Mar 24, 2006
Posts: 21
Thanks very much for your information

I agree. Here's the link:
subject: Interface Comparator
Similar Threads
Sorting java.lang.objects
making a mostly anonymous Comparator be Serializable
determine the input type of the value of the function parameter map
TreeSet and TreeMap
Comparable interface problem