[ACCEPTED]-When should a class be Comparable and/or Comparator?-comparable

Accepted answer
Score: 243

The text below comes from Comparator vs Comparable

Comparable

A comparable object 8 is capable of comparing itself with another 7 object. The class itself must implements 6 the java.lang.Comparable interface in order to be able to compare 5 its instances.

Comparator

A comparator object is capable 4 of comparing two different objects. The 3 class is not comparing its instances, but 2 some other class’s instances. This comparator 1 class must implement the java.util.Comparator interface.

Score: 160

Implementing Comparable means "I can compare myself with another object." This is typically 6 useful when there's a single natural default 5 comparison.

Implementing Comparator means "I can compare two other objects." This is 4 typically useful when there are multiple 3 ways of comparing two instances of a type 2 - e.g. you could compare people by age, name 1 etc.

Score: 38

Comparable lets a class implement its own 14 comparison:

  • it's in the same class (it is often an advantage)
  • there can be only one implementation (so you can't use that if you want two different cases)

By comparison, Comparator is 13 an external comparison:

  • it is typically in a unique instance (either in the same class or in another place)
  • you name each implementation with the way you want to sort things
  • you can provide comparators for classes that you do not control
  • the implementation is usable even if the first object is null

In both implementations, you 12 can still choose to what you want to be compared. With generics, you can declare 11 so, and have it checked at compile-time. This 10 improves safety, but it is also a challenge 9 to determine the appropriate value.

As a 8 guideline, I generally use the most general 7 class or interface to which that object 6 could be compared, in all use cases I envision... Not 5 very precise a definition though ! :-(

  • Comparable<Object> lets you use it in all codes at compile-time (which is good if needed, or bad if not and you loose the compile-time error) ; your implementation has to cope with objects, and cast as needed but in a robust way.
  • Comparable<Itself> is very strict on the contrary.

Funny, when 4 you subclass Itself to Subclass, Subclass 3 must also be Comparable and be robust about 2 it (or it would break Liskov Principle, and 1 give you runtime errors).

Score: 21

java.lang.Comparable

  1. To implement Comparable interface, class must 5 implement a single method compareTo()

    int a.compareTo(b)

  2. You must modify 4 the class whose instance you want to sort. So 3 that only one sort sequence can be created 2 per class.

java.util.Comparator

  1. To implement Comparator interface, class 1 must implement a single method compare()

    int compare (a,b)

  2. You build a class separate from class whose instance you want to sort. So that multiple sort sequence can be created per class.
Score: 14

Comparable is for providing a default ordering on 3 data objects, for example if the data objects 2 have a natural order.

A Comparator represents the ordering 1 itself for a specific use.

Score: 8

Comparable is usually preferred. But sometimes a class 6 already implements Comparable, but you want to sort 5 on a different property. Then you're forced 4 to use a Comparator.

Some classes actually provide 3 Comparators for common cases; for instance, Strings are by 2 default case-sensitive when sorted, but 1 there is also a static Comparator called CASE_INSENSITIVE_ORDER.

Score: 6

here are few differences between Comparator 21 and Comparable I found on web :

  1. If you see 20 then logical difference between these two 19 is Comparator in Java compare two objects 18 provided to him, while Comparable interface 17 compares "this" reference with 16 the object specified.

  2. Comparable in Java 15 is used to implement natural ordering of 14 object. In Java API String, Date and wrapper 13 classes implement Comparable interface.

  3. If 12 any class implement Comparable interface 11 in Java then collection of that object either 10 List or Array can be sorted automatically 9 by using Collections.sort() or Array.sort() method 8 and object will be sorted based on there 7 natural order defined by CompareTo method.

  4. Objects 6 which implement Comparable in Java can 5 be used as keys in a sorted map or elements 4 in a sorted set for example TreeSet, without 3 specifying any Comparator.

site:How to use 2 Comparator and Comparable in Java? With 1 example

Read more: How to use Comparator and Comparable in Java? With example

Score: 5

Comparable is for objects with a natural ordering. The 3 object itself knows how it is to be ordered.
Comparator is 2 for objects without a natural ordering or 1 when you wish to use a different ordering.

Score: 4

Difference between Comparator and Comparable interfaces

Comparable is used to compare itself by using with 2 another object.

Comparator is used to compare two datatypes 1 are objects.

Score: 2

If you see then logical difference between 15 these two is Comparator in Java compare two objects 14 provided to him, while Comparable interface compares 13 "this" reference with the object specified.

Comparable in 12 Java is used to implement natural ordering 11 of object. In Java API String, Date and 10 wrapper classes implement Comparable interface.

If 9 any class implement Comparable interface in Java then 8 collection of that object either List or Array can 7 be sorted automatically by using Collections.sort() or Array.sort() method 6 and object will be sorted based on there 5 natural order defined by compareTo method.

Objects 4 which implement Comparable in Java can be used as 3 keys in a sorted map or elements in a sorted 2 set for example TreeSet, without specifying any 1 Comparator.

Score: 0

My annotation lib for implementing Comparable 2 and Comparator:

public class Person implements Comparable<Person> {         
    private String firstName;  
    private String lastName;         
    private int age;         
    private char gentle;         

    @Override         
    @CompaProperties({ @CompaProperty(property = "lastName"),              
        @CompaProperty(property = "age",  order = Order.DSC) })           
    public int compareTo(Person person) {                 
        return Compamatic.doComparasion(this, person);         
    }  
} 

Click the link to see more 1 examples. compamatic

More Related questions