?
Solved

Question for CEHJ- Comparable interface

Posted on 2003-03-03
13
Medium Priority
?
239 Views
Last Modified: 2010-03-31
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
Comment
Question by:LearningJava
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8060617
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
 

Author Comment

by:LearningJava
ID: 8061462
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8063367
>>Is this correct?

Yes - apart from the last word - should be 'method' ;-)
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:LearningJava
ID: 8064028
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8064061
>>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
 

Author Comment

by:LearningJava
ID: 8064373
>>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
 

Author Comment

by:LearningJava
ID: 8064403
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8066005
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8066018
>>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
 

Author Comment

by:LearningJava
ID: 8071395
If Comparable does the job then why do we need the Comparator iterface?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8071416
Because it's required by binarySearch. *It* doesn't know there's a Comparable buried in there.
0
 

Author Comment

by:LearningJava
ID: 8071801
Your are the man!

Thanks for the help.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1200 total points
ID: 8071826
;-)
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month13 days, 7 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question