aspose file tools
The moose likes Java in General and the fly likes Doubt about  Comparator  interface Implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Doubt about  Comparator  interface Implementation " Watch "Doubt about  Comparator  interface Implementation " New topic
Author

Doubt about Comparator interface Implementation

Thennam Pandian
Ranch Hand

Joined: Oct 11, 2005
Posts: 163
When u implement a interface u should implement all it's methods.

But when we implement Comparator interface u can leave the equals()

method . where this method is implemented? (A Interface can't

implement it's method ...)

please Help me ............
Greg Charles
Bartender

Joined: Oct 01, 2001
Posts: 2542
    
  10

The Comparator interface only contains one method: compareTo(Object o). That's all you need to implement for the compiler to be happy. However, you would normally implement equals() so that compareTo() returns 0 if and only if equals() returns true.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Greg Charles:
The Comparator interface only contains one method: compareTo(Object o). That's all you need to implement for the compiler to be happy. However, you would normally implement equals() so that compareTo() returns 0 if and only if equals() returns true.


I think you're confusing Comparator with Comparable. It's easy to do that!


There is no emoticon for what I am feeling!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

And in answer to the original question: when you write a class that implements Comparator, the equals(Object) method is implemented by that class. If you don't explicitly write the code for it, then you inherit the default equals(Object) implementation from Object. In either case your class implements the method.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Thennam Pandian:
When you implement an interface you should implement all its methods.
But when we implement Comparator interface you can leave the equals()
method [unimplemented]. where this method is implemented? (A Interface can't
implement it's method ...)


Realize that the interface is repeating the equals method of class Object, but with a refined specification:

Indicates whether some other object is "equal to" this Comparator. This method must obey the general contract of Object.equals(Object). Additionally, this method can return true only if the specified Object is also a comparator and it imposes the same ordering as this comparator. Thus, comp1.equals(comp2) implies that sgn(comp1.compare(o1, o2))==sgn(comp2.compare(o1, o2)) for every object reference o1 and o2.

Note that it is always safe not to override Object.equals(Object). However, overriding this method may, in some cases, improve performance by allowing programs to determine that two distinct Comparators impose the same order.


When your Comparator doesn't define it's own equals method, it's inheriting this method from Object.

As another example of an interface repeating an Object method with more to its specification, take a look at equals() and hashCode() in java.util.List.
Greg Charles
Bartender

Joined: Oct 01, 2001
Posts: 2542
    
  10

Originally posted by Jeff Albrechtsen:


I think you're confusing Comparator with Comparable. It's easy to do that!




Note to self: stay out of the forums until this head cold clears.

I'm off to recheck every line of code I wrote today.
Thennam Pandian
Ranch Hand

Joined: Oct 11, 2005
Posts: 163
Originally posted by Paul Clapham:
And in answer to the original question: when you write a class that implements Comparator, the equals(Object) method is implemented by that class. If you don't explicitly write the code for it, then you inherit the default equals(Object) implementation from Object. In either case your class implements the method.



now i understand it ..........thankz for ur idea......
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt about Comparator interface Implementation
 
Similar Threads
sorting VO object sorting based on the attribute
Plz xplain this code on TreeSet
Comparator and TreeSet
Sorting Collection of ArrayList
Comparator() error