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.compare To(student 2.firstNam e);
}
Thanks
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.compare
}
Thanks
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.
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?
>>Is this correct?
Yes - apart from the last word - should be 'method' ;-)
Yes - apart from the last word - should be 'method' ;-)
ASKER
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.compare To(student 2.firstNam e);
}
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.
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.compare
}
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)
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)
ASKER
>>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?
"From Comparable, an interface implemented by String"
How is it possible to implement the Comparable interface when we have only implemented the Comparator interface?
ASKER
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.compare To(student 2.firstNam e);
}
Comparator FirstComp = new FirstNameComp();
Arrays.sort(people, lastComp);
class FirstNameComp implements Comparator {
public int compare(Object obj1, Object obj2) {
Student student1 = (Student) obj1;
Student student2 = (Student) obj2;
return student1.firstName.compare
}
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).
*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.
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.
ASKER
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.
ASKER
Your are the man!
Thanks for the help.
Thanks for the help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.