This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Comparator interface 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 "Comparator interface" Watch "Comparator interface" New topic
Author

Comparator interface

Yogesh Baraskar
Ranch Hand

Joined: Oct 07, 2007
Posts: 33
Hi

I ran javap java.util.Comparator and got the results as:
Compiled from "Comparator.java"
public interface java.util.Comparator{
public abstract int compare(java.lang.Object, java.lang.Object);
public abstract boolean equals(java.lang.Object);
}

IN K and B book at page number 554 line 7 says that
The comparator interface has only one method compare()

but in interface we have equals() method also.

Then i tried
class ListSorting implements Comparator<String>
{
public int compare(String ob1, String ob2){
return ob2.compareTo(ob1);
}
}


It got compiled with any error.

Where is the equals() method?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The equals method is part of the interface. It overrides the one inherited from Object. You can write your own implementation of equals. But you won't get a compile-time error if you don't override it with your own.
Yogesh Baraskar
Ranch Hand

Joined: Oct 07, 2007
Posts: 33
Hi Keith,

public abstract boolean equals(java.lang.Object);

it has abstract keyword , that means we must provide implementation in the class which implements Comparator interface?
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Keith Lynn:
The equals method is part of the interface. It overrides the one inherited from Object.


Technically, that statement's not quite right. The interface does not "override" the equals() method from Object. Rather, it simply mandates that any object that implements Comparator must provide an equals() method. However, since Object already has an appropriate equals() method, no other class ever has to provide its own implementation of equals() to satisfy the Comparator interface.

Why, then, does Comparator specify an equals() method? You'll have to ask the original designers for the complete answer, but the Javadocs imply that this was just to draw programmers' attention to this recommendation:

public boolean equals(Object obj)

Indicates whether some other object is "equal to" this Comparator. This method must obey the general contract of Object.equals(Object). . . . 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.


SCJP 5.0
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Sorry. I didn't meant to confuse. I was using overrides in the sense that the javadocs do.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Comparator interface
 
Similar Threads
Any benefit by using StringBuffer like this?
TreeSet Question
compare
generics...
Sets