Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

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
0
LearningJava
Asked:
LearningJava
  • 7
  • 6
1 Solution
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now