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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Couple of doubts in Comparator Interface" Watch "Couple of doubts in Comparator Interface" New topic
Author

Couple of doubts in Comparator Interface

Parthiban Malayandi
Ranch Hand

Joined: Apr 15, 2008
Posts: 70
Hi all ,
I'm studying Collections for SCJP .I have a doubt in Comparator interface .

While looking at the API I found that it contains two methods :

1.compare
2.equals

Here's where I'm stuck . while using Comparator mostly we are advised to (and most of the time only )override compare method but being a method in interface why we are not forced to override equals method ?

How it compiles successfully without overriding one method of interface ?

I also found it's because of equals method overrides from Object's class(if I understood correctly from API documentation )

what I understood is correct ?

It even confused me more .

How a interface extends a class(Object) ? (will it ?)
How a interface give implementation for a method ?

Please clarify my doubts .

Thanks in advance
Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
Hey Parthiban,

the equlas() method is only to compare different instances of Comparator implementations. For sorting data, it definetly suffices to implement the compare() mathod. I use the Comparator quite often and have never implemented equals() so far. So, why is it there and why don't we have to implement it? Well firstly, as you can see, the method signature is the same as the one from java.lang.Object. Since each class you implement inherits from Object, there is always a valid implementation matching the one from the Comparator interface. So, why is it there at all? If you read the API doc for Comparators equals() method carefully, you'll find that the interface specifies additional bahavior diverging from the Object's equals() specification. Therefore, the method is listed there. You don't have to implement equals(), but if you do, you have to fulfill the specification from the Comparator interface.

Hope this helps,
Thomas


SCJP 5.0, SCJD in progress
Mohammad Khan
Ranch Hand

Joined: Sep 23, 2008
Posts: 37
Following code my help to understand the hidden issue here !


Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
One thing I forgot to mention:
Although the Object's specification of equals() has been changed by the Comparator interface, the hashCode()'s specification has not. Therefore and as always, to not break the general contract of Object's hashCode(), you'll have to override hashCode() in case you decide to override equals().
Parthiban Malayandi
Ranch Hand

Joined: Apr 15, 2008
Posts: 70
Originally posted by Thomas Thevis:
Hey Parthiban,

the equlas() method is only to compare different instances of Comparator implementations. For sorting data, it definetly suffices to implement the compare() mathod. I use the Comparator quite often and have never implemented equals() so far. So, why is it there and why don't we have to implement it? Well firstly, as you can see, the method signature is the same as the one from java.lang.Object. Since each class you implement inherits from Object, there is always a valid implementation matching the one from the Comparator interface. So, why is it there at all? If you read the API doc for Comparators equals() method carefully, you'll find that the interface specifies additional bahavior diverging from the Object's equals() specification. Therefore, the method is listed there. You don't have to implement equals(), but if you do, you have to fulfill the specification from the Comparator interface.

Hope this helps,
Thomas


Hi Thomas and Mohammad,
Thanks for your replies now I understood why it worked and mentioned in API.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Couple of doubts in Comparator Interface