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
LearningJavaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
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.
0
LearningJavaAuthor Commented:
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?
0
CEHJCommented:
>>Is this correct?

Yes - apart from the last word - should be 'method' ;-)
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

LearningJavaAuthor Commented:
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.
0
CEHJCommented:
>>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)

0
LearningJavaAuthor Commented:
>>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?



0
LearningJavaAuthor Commented:
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);
0
CEHJCommented:
>>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).
0
CEHJCommented:
>>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.
0
LearningJavaAuthor Commented:
If Comparable does the job then why do we need the Comparator iterface?
0
CEHJCommented:
Because it's required by binarySearch. *It* doesn't know there's a Comparable buried in there.
0
LearningJavaAuthor Commented:
Your are the man!

Thanks for the help.
0
CEHJCommented:
;-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.