aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interface Comparator" Watch "Interface Comparator" New topic
Author

Interface Comparator

Eleanor Leong
Greenhorn

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

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:

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

See also:

http://www.velocityreviews.com/forums/t145137-comparatorequals.html


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

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

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