• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

compareTo()

 
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.,...............................
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Banu can you explain a bit more clearly what are you trying to ask...
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
     
    Balaji Bang
    Ranch Hand
    Posts: 182
    Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 182
    Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Please explain me when it is reverse order and when it is natural order....................
     
    Ranch Hand
    Posts: 580
    Eclipse IDE
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    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.
    reply
      Bookmark Topic Watch Topic
    • New Topic