aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Comparable interface problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Comparable interface problem" Watch "Comparable interface problem" New topic
Author

Comparable interface problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

KB book page-573,574
topic Sorting with Comparator



The other handy thing about
the Comparator interface is that you can use it to sort instances of any class—even
classes you can't modify—unlike the Comparable interface, which forces you to
change the class whose instances you want to sort.


i just cannot understand that while using Comparable interface how are we changing the class
for instance:


IN The compareTo function definition in DVDInfo ,only the compareTo function is called with DVDInfo objects.now,where are we changing the class.


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Please ShowSomeEffort. You can find a lot of tutorials showing you how the Comparable and Comparator interfaces work.


[My Blog]
All roads lead to JavaRanch
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

sorry Christophe.
i have updated my query
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

You already know that you can use easy Comparable or Comparator to sort your DVDInfo. If you use the Comparable interface, you have to change the DVDInfo class, by implementing the Comparable interface and its compareTo method. However, if you use a Comparator, you don't need to touch your DVDInfo class. This is more flexible, because you can make as many comparators as you want.

Kevin Kilbane
Ranch Hand

Joined: Sep 22, 2008
Posts: 42
mohitkumar gupta wrote:The other handy thing about
the Comparator interface is that you can use it to sort instances of any class—even
classes you can't modify—unlike the Comparable interface, which forces you to
change the class whose instances you want to sort.

i just cannot understand that while using Comparable interface how are we changing the class


Say you have 2 String objects that you want to compare. String implements Comparable which means it has a compareTo method that compares Strings alphabetically so "Apple" comes before "Bed" which comes before "Cork".

Say, for whatever reason, in your program that you want Strings to be ordered by the length of the String i.e. "Bed" (3 characters) comes before "Cork" (4 characters) which comes before "Apple" (5 characters).

You do not have access to the String class - you can't modify the compareTo method of String.java.

But you can write a new class




Hoever, if you have objects of your own class you want to compare - you can do it by modifying your own class:



So in the first example you didn't modify the class you wanted to compare (String class) but in the second example, you could modify it (MyClass class).
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

i have understood what you are trying to say



but ,i think return of the compare function should be

and in the second case when the comparable interface is implemented,the function to be used should be

Kevin Kilbane
Ranch Hand

Joined: Sep 22, 2008
Posts: 42
mohitkumar gupta wrote:i have understood what you are trying to say



but ,i think return of the compare function should be

and in the second case when the comparable interface is implemented,the function to be used should be



Incorrect I'm afraid.

s1.length and age are both ints so you they don't have compareTo methods to call (they don't have ANY methods to call).

Even if s1.length and age weren't ints, if they were objects of some type, it doesn't necessarily mean you have to use (s1 and age's) compareTo methods within the compare / compareTo methods (of StringLengthComparator and MyClass). It depends totally on the design of your program and what attributes of an object you need to sort on.
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

1. class MyClass implements Comparable {
2. int age;
3.
4. public int compare(MyClass mc) {
5. return this.age - mc.age;
6. }
7. }



compare function takes two arguments and not one as mentioned in java API.
i think the compare function definition is wrong
Kevin Kilbane
Ranch Hand

Joined: Sep 22, 2008
Posts: 42
mohitkumar gupta wrote:
1. class MyClass implements Comparable {
2. int age;
3.
4. public int compare(MyClass mc) {
5. return this.age - mc.age;
6. }
7. }



compare function takes two arguments and not one as mentioned in java API.
i think the compare function definition is wrong


That is correct but a class that implements Comparable should over-write compareTo, not compare. My example above is wrong, it should have read:



Apologies for confusion
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparable interface problem