Forums Register Login

Couple of doubts in Comparator Interface

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Following code my help to understand the hidden issue here !


+Pie Number of slices to send: Send
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().
+Pie Number of slices to send: Send
 

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.
Liar, liar, pants on fire! refreshing plug:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 960 times.
Similar Threads
i have not override comparator's equals() method(overrided compare())-working fine-how?
Overriding the equlas method
Doubt about Comparator interface Implementation
Comparable and Comparator
Sorting an ArrayList of Object X
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 18:12:58.