difference between comparator and comparable interface

Discussion in 'Java' started by gaurav, Feb 20, 2005.

  1. gaurav

    gaurav Guest

    what's the difference between comparable and comparator interface.
    gaurav, Feb 20, 2005
    #1
    1. Advertising

  2. gaurav wrote:
    > what's the difference between comparable and comparator interface.


    Comparable interface shows that the class knows how to compare ITSELF
    against another class.

    Comparator interface allows a single class know how to compare two
    classes which (typically) are not the same type as the comparator.


    Its about moving the responsibility for doing the comparison from the
    class that is being compared, to another class who's sole responsibility
    is to be the comparator.
    Andrew McDonagh, Feb 20, 2005
    #2
    1. Advertising

  3. On 20 Feb 2005 04:12:12 -0800, gaurav <> wrote:

    > what's the difference between comparable and comparator interface.


    Something that's Comparable knows how to compare itself to others
    (presumably of the same class). How it does so is built into the class.
    For example, Strings compare themselves alphabetically (well, by character
    value, anyway). A Comparator can compare two other objects by whatever
    criterion the Comparator chooses. For example, it might compare Strings by
    their length.
    HTH,
    Michiel
    Michiel Konstapel, Feb 20, 2005
    #3
  4. gaurav

    Oscar kind Guest

    In comp.lang.java.help Michiel Konstapel <> wrote:
    > On 20 Feb 2005 04:12:12 -0800, gaurav <> wrote:
    >
    >> what's the difference between comparable and comparator interface.

    >
    > Something that's Comparable knows how to compare itself to others
    > (presumably of the same class). How it does so is built into the class.
    > For example, Strings compare themselves alphabetically (well, by character
    > value, anyway). A Comparator can compare two other objects by whatever
    > criterion the Comparator chooses. For example, it might compare Strings by
    > their length.


    The Comparable interface is (IMO) best used to implement a natural order
    for lists of elements of the class. For example the low-to-high order of
    numbers represented by the Integer class. This ties in nicely with the
    order used by the variants of TreeSet, TreeMap, Arrays.sort() and
    Collections.sort() where no Compataror is specified.

    The Comparator interface is then used to create exotic orders, where
    exotic is anything not implemented by the Comparable interface. This also
    means the most obvious order for elements if a class doesn't implement
    Comparable, though a subclass may sometimes be a better choice then.


    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
    Oscar kind, Feb 20, 2005
    #4
  5. gaurav

    vijay24805

    Joined:
    Apr 14, 2009
    Messages:
    1
    Java.util.comparatable And Comparator

    In compareto(object 1,obect 2) and compare() method the object ar comapared and returned -ve,0 and +ve values based on the comparision --- if object1 is greater than object 2 return 0,my question in what sense the object ar compared as greater or smaller?is it the no. of characters counted and compared?

    ex-obj1 let be "fun"
    and obj2 let be "hello" then if we compare this two object by using overriding of
    compare(obj1,obj2)
    {
    return obj1.compareTo(obj2);
    }
    how i can preddict wether it is -1,1 or zero?
    vijay24805, Apr 14, 2009
    #5
    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. jakk
    Replies:
    4
    Views:
    12,036
  2. Replies:
    6
    Views:
    8,442
    Stefan Ram
    Sep 20, 2005
  3. Tuurbo46

    Comparable interface

    Tuurbo46, Oct 25, 2005, in forum: Java
    Replies:
    9
    Views:
    3,621
    Chris Smith
    Oct 26, 2005
  4. SD
    Replies:
    2
    Views:
    8,028
    Roedy Green
    May 22, 2006
  5. Chanchal

    Comparable interface

    Chanchal, Apr 23, 2007, in forum: Java
    Replies:
    6
    Views:
    443
    Daniel Pitts
    Apr 23, 2007
Loading...

Share This Page