File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes compareTo() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "compareTo()" Watch "compareTo()" New topic
Author

compareTo()

Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 180
compareTo(Object o);

And in compare we use :
i.compareTo((Dog)o.i);

Which algorithm it uses>???

I am totally confused to find the order based on these methods...

Anyone please explain.,...............................
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Banu can you explain a bit more clearly what are you trying to ask...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Henry Zhi Lin
Ranch Hand

Joined: Nov 04, 2008
Posts: 69
Banu Chowdary wrote:compareTo(Object o);



Which algorithm it uses>???

I am totally confused to find the order based on these methods...

Anyone please explain.,...............................


I think you will have to define your own algorithm when you implement this interface


SCJP 5.0
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
the compareTo() is the sole method of the Comparable interface in the collections framework, and is implemented on objects within a particular collection classes to implement equality checking, it is similar in part to the .equals() method of the Object class, which in good practice should always be overriden if used (in addition to the hashCode() method of the Object class.

According to the Api, overriding the compareTo() method of the Comparable interface the following contract must be honored:

  • anti-commutation : x.compareTo(y) is the opposite sign of y.compareTo(x) .


  • exception symmetry : x.compareTo(y) throws exactly the same exceptions as y.compareTo(x) .


  • transitivity : if x.compareTo(y)>0 and y.compareTo(z)>0, then x.compareTo(z)>0 (and same for less than) .


  • if x.compareTo(y)==0, then x.compareTo(z) has the same sign as y.compareTo(z) .


  • consistency with equals is highly recommended, but not required : x.compareTo(y)==0, if and only if x.equals(y) ; consistency with equals is required for ensuring
    sorted collections (such as TreeSet) are well-behaved.



  • Take a look at the APi and the Sun Collections tutorial.

    With your particular example the expression:



    the reference i is referring to an Object (or variable of) which implements the Comparable interface. It is calling the compareTo() method (which should be overridden). The method is taking an Object reference 'o' and casting it to a Dog Object Type, and then using the 'dot' (.)notation to call the variable i of the o Object. Thus the algorithm compares the value contained in the reference i of the comparator Object with the value of i of the o object (which is being cast as a Dog object).

    I am unsure if the guide applies here, but when overriding the Object.equals() method, it is good practice to add a further check the type of Object with the 'instanceo'f operator o' before Casting, otherwise there is a danger of typecast exceptions and such like for example (in the overridden equals method, if used in your example:



    Again, I'm unsure as to whether this design reccomendation applies to compareTo(), but I assume it would be a good error-check if Class-casting is involved. Perhaps, one of our wise moderators can offer some direction on this?

    Hope this helps!

    be a well encapsulated person, don't expose your privates, unless you public void getWife()!
    Balaji Bang
    Ranch Hand

    Joined: Apr 23, 2007
    Posts: 180
    public int compareTo(Object o){
    Dog d=(Dog)o;
    ++count;
    System.out.println(count+"i "+i); // Line 1
    System.out.println(count+"d.i "+d.i);//Line 2
    return i-d.i;
    }

    When adding some object to TreeSet we should implemenet either Comparable or we should pass comparator to the TreeSet constructor.. Right???

    I have 3 dog objects in TreeSet. new Dog(1), new Dog(2), new Dog(3)....
    And in line 1, line 2 I am printing i and d.i
    Her e I didnot understand how it is taking i and d.i and comparing..

    Any one please explain.....

    If it returns -1 it is decreasing order .. And if +1 means increasing order. 0 means equal right???
    Balaji Bang
    Ranch Hand

    Joined: Apr 23, 2007
    Posts: 180


    Please explain me when it is reverse order and when it is natural order....................
    James Tharakan
    Ranch Hand

    Joined: Aug 29, 2008
    Posts: 580



    Here a.name is the calling object(the object which is calling the method)and the name is the argument.
    if the calling object is greater than the agrument then the method returns positive number.

    If the argument passed to the method(marked blod below) is used to call the compareTo () then it is in reverse order
    public int compareTo(Animal a){
    return a.name.compareTo(name);
    }

    Hope i am clear.


    SCJP 6
    Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: compareTo()