Dear friends
I am confuse in that When to use Comparator and Comparable Interface in java. Please provide me the solutions with an example.
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.
Dear friends
I am confuse in that When to use Comparator and Comparable Interface in java. Please provide me the solutions with an example.
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!
<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!
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
-------------------------------------------------------------------------------------
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!
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.
Last edited by copeg; October 26th, 2012 at 09:35 AM. Reason: link removed
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