Link to home
Start Free TrialLog in
Avatar of LearningJava
LearningJava

asked on

Question for CEHJ- Comparable interface

Hi:
   Thanks for clarifying the answer to my last question and hopeful you can help me with another question.

In a  previous question I was required to implement the Comparable interface.

Why was the compare method used and not the compareTo method?

class FirstNameComp implements Comparator {
public int compare(Object obj1, Object obj2) {
Student student1 = (Student) obj1;
Student student2 = (Student) obj2;
return student1.firstName.compareTo(student2.firstName);
}

Thanks
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

The reason is that you were using the binarySearch method of the Arrays class that took as an argument a reference to an implementation of the Comparator interface. This interface has two methods, one of which is compare, which compares two objects for equality, according to criteria you choose. In this case, since the criterion was the equality of the first name of Student, it was convenient to use the compareTo method of the class String, from another interface, Comparable which in this case compares using the natural ordering of String.
Avatar of LearningJava
LearningJava

ASKER

I see my mistake. Comparable and Comparator are two entirely different interfaces. If I use the Comparable interface then I override the compareTo method. If I use the Comparator interface then I override the compare interface.

Is this correct?
>>Is this correct?

Yes - apart from the last word - should be 'method' ;-)
Hi:
   I need further clarification?

class FirstNameComp implements Comparator {
public int compare(Object obj1, Object obj2) {
Student student1 = (Student) obj1;
Student student2 = (Student) obj2;
return student1.firstName.compareTo(student2.firstName);
}


I am trying to follow the logic of the method. Is this correct:

1)implement class Comparator
2)override the compare method
3)invoke student objects. At this point is the compare method doing anything?
4)Use the compareTo method to compare the first name of each student object. Where does the compareTo method come from?What class?

What are we returning?

Thanks.
>>3)invoke student objects. At this point is the compare method doing anything?

No. You're just casting for the next bit.

>>Where does the compareTo method come from?What class?

From Comparable, an interface implemented by String

>>What are we returning?

An integer depending on the result of compareTo. See the docs: http://java.sun.com/j2se/1.4/docs/api/java/lang/Comparable.html#compareTo(java.lang.Object)

>>Where does the compareTo method come from?What class?

"From Comparable, an interface implemented by String"

How is it possible to implement the Comparable interface when we have only implemented the Comparator interface?



I realize that an integer is returned(-1,0,1) but how does that work with a sort array?


class FirstNameComp implements Comparator {
public int compare(Object obj1, Object obj2) {
Student student1 = (Student) obj1;
Student student2 = (Student) obj2;
return student1.firstName.compareTo(student2.firstName);
}




Comparator FirstComp = new FirstNameComp();
Arrays.sort(people, lastComp);
>>How is it possible to implement the Comparable interface when we have only implemented the Comparator interface?

*We* don't implement Comparable, String does (see docs).
>>I realize that an integer is returned(-1,0,1) but how does that work with a sort array?

The point is, Comparable, implemented by String itself, returns values in exactly the same way with respect to natural ordering, so we just use this functionality that's already there in String, as it's Strings we're comparing.
If Comparable does the job then why do we need the Comparator iterface?
Because it's required by binarySearch. *It* doesn't know there's a Comparable buried in there.
Your are the man!

Thanks for the help.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial