Comparable interface

Discussion in 'Java' started by Tuurbo46, Oct 25, 2005.

  1. Tuurbo46

    Tuurbo46 Guest

    Hi

    Im currently struggling to covert an existing base class to a comparable
    interface - i cant find a web page with a good example. Please could
    somebody point me in the right direction?

    Also, does anybody recommend a really good web site which covers the more
    advanced concepts of java?

    Thanks Turboo
     
    Tuurbo46, Oct 25, 2005
    #1
    1. Advertising

  2. Tuurbo46

    Guest

    Well all you need to do is implement the Comparable interface which
    involves giving an implementation to compareTo.
     
    , Oct 25, 2005
    #2
    1. Advertising

  3. Tuurbo46

    Tuurbo46 Guest

    yeah, i understand that part, but i need an example to work to! Sorry, im
    quite new to java

    Turboo

    <> wrote in message
    news:...
    > Well all you need to do is implement the Comparable interface which
    > involves giving an implementation to compareTo.
    >
     
    Tuurbo46, Oct 25, 2005
    #3
  4. Tuurbo46

    Oliver Wong Guest

    <> wrote in message
    news:...
    > Well all you need to do is implement the Comparable interface which
    > involves giving an implementation to compareTo.


    If you implement the compareTo method, you should also override the
    equals() and hashcode(). You must also ensure that the relation imposed by
    the compareTo operation is a total ordering, which means you also have to
    take into account where null appears in the order.

    - Oliver
     
    Oliver Wong, Oct 25, 2005
    #4
  5. Tuurbo46

    Oliver Wong Guest

    "Tuurbo46" <> wrote in message
    news:djm0ni$m74$...
    > yeah, i understand that part, but i need an example to work to! Sorry, im
    > quite new to java
    >
    > Turboo
    >
    > <> wrote in message
    > news:...
    >> Well all you need to do is implement the Comparable interface which
    >> involves giving an implementation to compareTo.
    >>

    >


    Here's a short example (didn't verify this with a compiler, so might not
    compile):

    <code>
    /**
    * Represents an immutable integer.
    */
    class MyInteger implements Comparable<MyInteger> {
    private int myValue;

    public MyInteger(int value) {
    this.myValue = value;
    }

    public int getValue() {
    return this.myValue;
    }

    /**
    * @throws NullPointerException if o is null.
    */
    public int compareTo(MyInteger o) {
    if (this.myValue < o.myValue) {
    return -1;
    }
    if (this.myValue == o.myValue) {
    return 0;
    }
    assert this.myValue > o.myValue;
    return 1;
    }
    }
    </code>

    Note that you might see code to implement compareTo like this:

    <badCode>
    public int compareTo(MyInteger o) {
    return this.myValue - o.myValue;
    }
    </badCode>

    Using subtraction like this introduces a subtle bug involving integer
    overflow, so it's better to just use if statements like in the earlier
    example.

    - Oliver
     
    Oliver Wong, Oct 25, 2005
    #5
  6. On 2005-10-25, Tuurbo46 penned:
    >
    > Also, does anybody recommend a really good web site which covers the
    > more advanced concepts of java?
    >
    > Thanks Turboo


    I would recommend the book Thinking In Java, which is also available
    for free online.


    --
    monique

    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
     
    Monique Y. Mudama, Oct 25, 2005
    #6
  7. Tuurbo46

    Roedy Green Guest

    On Tue, 25 Oct 2005 20:08:45 +0100, "Tuurbo46" <>
    wrote, quoted or indirectly quoted someone who said :

    >Im currently struggling to covert an existing base class to a comparable
    >interface - i cant find a web page with a good example. Please could
    >somebody point me in the right direction?


    see http://mindprod.com/jgloss/Comparable.html

    My own site tends to focus on beginning and intermediate users.

    see http://mindprod.com/jgloss/jgloss.html

    Try the Sun forums for more advanced discussion.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Oct 26, 2005
    #7
  8. Tuurbo46

    Roedy Green Guest

    On Tue, 25 Oct 2005 20:08:45 +0100, "Tuurbo46" <>
    wrote, quoted or indirectly quoted someone who said :

    >
    >Also, does anybody recommend a really good web site which covers the more
    >advanced concepts of java?


    You describe yourself as a newbie but want the advanced stuff?? How
    about the intermediate?
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Oct 26, 2005
    #8
  9. Tuurbo46

    Roedy Green Guest

    On Tue, 25 Oct 2005 19:40:13 GMT, "Oliver Wong" <>
    wrote, quoted or indirectly quoted someone who said :

    >
    ><badCode>
    > public int compareTo(MyInteger o) {
    > return this.myValue - o.myValue;
    > }
    ></badCode>


    Granted that code fails in the general case, but when writing
    comparators, you often know for example that your ints are bounded in
    a range nowhere near the sign bit, so the subtraction code is quite
    safe.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Oct 26, 2005
    #9
  10. Tuurbo46

    Chris Smith Guest

    Tuurbo46 <> wrote:
    > Im currently struggling to covert an existing base class to a comparable
    > interface - i cant find a web page with a good example. Please could
    > somebody point me in the right direction?


    For most classes, which would just be ordered based on some set of
    fields in order of significance, there is a simple pattern.

    Before Java 5.0:

    public class A implements Comparable
    {
    private int a;
    private String b;
    private boolean c;

    public int compareTo(Object other)
    {
    A obj = (A) other;
    int result;

    result = new Integer(a).compareTo(new Integer(obj.a));
    if (result != 0) return result;

    result = b.compareTo(obj.b);
    if (result != 0) return result;

    result = c ? (obj.c ? 0 : 1) : (obj.c ? -1 : 0);
    if (result != 0) return result;

    return 0;
    }
    }

    Java 5.0:

    public class A implements Comparable<A>
    {
    private int a;
    private String b;
    private boolean c;

    public int compareTo(A obj)
    {
    int result;

    result = a.compareTo(obj.a);
    if (result != 0) return result;

    result = b.compareTo(obj.b);
    if (result != 0) return result;

    result = c ? (obj.c ? 0 : 1) : (obj.c ? -1 : 0);
    if (result != 0) return result;

    return 0;
    }
    }

    A few notes:

    1. If the comparison is simpler -- such as when sorting based on only
    one "key" field, where the other state is irrelevant for the natural
    ordering -- then the code can be simplified.

    2. If the ordering is more complex, you'll need to add to this form
    somewhat. For example, you might need to calculate intermediate values
    for each object, and then compare those.

    3. When comparing integer values, resist the temptation to just subtract
    one from the other, unless you know that they are constrained to less
    than half of the range of the resultant data type from that subtraction
    (int or long). There is a danger that an overflow will cause the wrong
    value to be returned.

    For the example above, if a = 2000000000, and
    obj.a = -2000000000, then the quantity (a - obj.a) will actually be
    negative due to overflow, and the code will fail. That's why I used
    Integer.compareTo instead of subtracting.

    Floating point numbers (float and double) don't suffer from this
    problem, but you should apply Math.signum to the result before casting
    it to int, to avoid certain potential problems.

    > Also, does anybody recommend a really good web site which covers the more
    > advanced concepts of java?


    For stuff like this, Josh Bloch's book is good. (I don't know if it
    specifically discusses this issue or not, but it addresses a lot of
    stuff like this.) I'm not aware of any web sites.

    On the other hand, the term "advanced concepts of java" i9s somewhat
    unclear, and I doubt many people would use it to describe implementing
    Comparable. Is there something different you had in mind.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Oct 26, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gaurav
    Replies:
    4
    Views:
    77,551
    vijay24805
    Apr 14, 2009
  2. Replies:
    6
    Views:
    8,546
    Stefan Ram
    Sep 20, 2005
  3. Chanchal

    Comparable interface

    Chanchal, Apr 23, 2007, in forum: Java
    Replies:
    6
    Views:
    480
    Daniel Pitts
    Apr 23, 2007
  4. vk
    Replies:
    16
    Views:
    1,876
    Alf P. Steinbach
    Feb 9, 2009
  5. Roedy Green
    Replies:
    16
    Views:
    649
Loading...

Share This Page