Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: When to use Comparator and Comparable Interface in java

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    32
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default When to use Comparator and Comparable Interface in java

    Dear friends


    I am confuse in that When to use Comparator and Comparable Interface in java. Please provide me the solutions with an example.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: When to use Comparator and Comparable Interface in java

    Why don't you come up with a few example programs that use each? If you post them as SSCCEs here, we can discuss what you find out.

    Alternatively, a google search of "java comparable vs comparator" came up with some promising results.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: When to use Comparator and Comparable Interface in java

    <bleedin obvious>
    If you try to call a method that accepts a Comparator as a parameter then you have to use a Comparator and not Comparable. And vice-versa.
    </bleedin obvious>
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: When to use Comparator and Comparable Interface in java

    Difference between Comparator and Comparable in Java
    1) Comparator in Java is defined in java.util package while Comparable interface in Java is defined in java.lang package.
    2) Comparator interface in Java has method public int compare (Object o1, Object o2) which returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. While Comparable interface has method public int compareTo(Object o) which returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
    3) If you see then logical difference between these two is Comparator in Java compare two objects provided to him, while Comparable interface compares "this" reference with the object specified.
    4) Comparable in Java is used to implement natural ordering of object. In Java API String, Date and wrapper classes implement Comparable interface.
    5) If any class implement Comparable interface in Java then collection of that object either List or Array can be sorted automatically by using Collections.sort() or Array.sort() method and object will be sorted based on there natural order defined by CompareTo method.
    6)Objects which implement Comparable in Java can be used as keys in a sorted map or elements in a sorted set for example TreeSet, without specifying any Comparator.

    Also Please find below the examples for using the Comparator and Comparable Interface

    1) There is class called Person, sort the Person based on person_id.
    2) Sort the Person based on Name.

    For a Person class sorting based on person_id can be treated as natural order sorting and sorting based on Name can be implemented using Comparator interface. To sort based on person_id we need to implement compareTo() method.


    public class Person implements Comparable {
    private int person_id;
    private String name;

    /**
    * Compare current person with specified person
    * return zero if person_id for both person is same
    * return negative if current person_id is less than specified one
    * return positive if specified person_id is greater than specified one
    */
    public int compareTo(Person o) {
    return this.person_id - o.person_id ;
    }
    ….
    }

    And for sorting based on person name we can implement compare (Object o1, Object o2) method of Comparator in Java or Java Comparator class.

    public class PersonSortByPerson_ID implements Comparator{

    public int compare(Person o1, Person o2) {
    return o1.getPersonId() - o2.getPersonId();
    }
    }


    Thanks and Regards,
    -------------------------------------------------------------------------------------
    Devki Kukde | dkukde@infocepts.com | www.infocepts.com
    -------------------------------------------------------------------------------------

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: When to use Comparator and Comparable Interface in java

    Quote Originally Posted by devkikukde View Post
    4) Comparable in Java is used to implement natural ordering of object. In Java API String, Date and wrapper classes implement Comparable interface.
    5) If any class implement Comparable interface in Java then collection of that object either List or Array can be sorted automatically by using Collections.sort() or Array.sort() method and object will be sorted based on there natural order defined by CompareTo method.
    These two are just as easily done with a Comparator.

    By the way, the links in your post are pretty close to spam- I'd not add them to future posts, if I were you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    18
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: When to use Comparator and Comparable Interface in java

    There are several difference between these 2 as mentioned by "devkikukde". I would rather focus on when we should use one of them.

    1- There is a scenario when you can not modify the object(Third party) in the question then you are left with only one option and that is "Comparator" because in order to use comparable your class needs to implements this interface. In simple word if you want to provide external order behvior you should go for comparator.

    2- With comparator you can provide more then 1 order behavior while this is not true for comparable.

    3- Comparable is used when your want to provide default ordering behaviour of the object in question.

  7. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: When to use Comparator and Comparable Interface in java

    Quote Originally Posted by diyaots View Post
    Dear friends


    I am confuse in that When to use Comparator and Comparable Interface in java. Please provide me the solutions with an example.
    What is comparable and comparator and when to use comparable and comparator. To know these read this link. this will help you to understand their behavior and usage.

    ...link removed
    Last edited by copeg; October 26th, 2012 at 09:35 AM. Reason: link removed

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: When to use Comparator and Comparable Interface in java

    Please don't resurrect a post that is over a year and a half old, especially to post a link to an external site - presumably to promote your own website. Doing so quickly places you into the category of posters called spam

Similar Threads

  1. Java web console with Java Native interface
    By rcbandit in forum Java Native Interface
    Replies: 1
    Last Post: August 22nd, 2011, 08:12 AM
  2. Priority Queue using comparable
    By jkalm in forum Collections and Generics
    Replies: 6
    Last Post: December 5th, 2010, 10:02 PM
  3. Comparable Interface
    By yelrubk in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 09:08 AM
  4. Replies: 0
    Last Post: October 2nd, 2009, 10:51 PM
  5. [SOLVED] help with sorting...(comparator)
    By mdstrauss in forum Collections and Generics
    Replies: 2
    Last Post: July 26th, 2009, 06:25 AM

Tags for this Thread