Comparable

In Java, this interface is implemented by a class to indicate that it can be compared to another object and therefore ordered.

Comparator

A Common interface to create an object that specifies the way of comparing other objects



Econd judge implement

Example

"A comparator class is better since use of comparable would mean using"

from question  

Deciding to use Comparable or Comparator

"But if you implement comparable interface you will have to change code of all the model bean classes to override comparator method;so for loose coupling comparator is better"

from question  

In what way is a comparator superior to comparable?

"Econd you can implement a comparator to judge which is bigger;if you don t want the object to be comparable which means you don t want to change the existing code to let the existing code implement comparable this is a better way"

from question  

Remove elements from collection

Objects new useful

Example

"You save memory by not creating new comparator objects and comparator is useful if you are comparing objects of different types which is not your case;your listdata object should implements comparable not comparator interface"

from question  

Sorting custom object array on two fields

"Comparable which has comparator requires the objects to be compared in order to use a treemap or to sort a list to implement that interface;then you have to implement a comparator which is a bit less convenient to use"

from question  

What is the difference between compare() and compareTo()?

"2 a priorityqueue can sort its elements either by a comparable s object comparator method or using a comparator for objects that are not necessarily comparable;the siftdowncomparable method is only invoked if a comparator was not provided when the priorityqueue was created"

from question  

Java PriorityQueue implementation: Why Object[] queue instead of E[] queue? What's the purpose of "key" in siftUp/siftDownComparable?

"If the ordering of an object is an implementation detail of the object the comparable is more appropriate;if the ordering of the objects is controlled by the caller then comparator is more appropriate"

from question  

Why does the Java Collections Framework offer two different ways to sort?

Sort clear natural

Example

"That said using a standard sort with either comparable rows or a comparator is much better than mixing up the sort logic and the comparison logic;comparator is the more flexible way"

from question  

Java sort based on two columns - Without comparator

"N object should implement comparable if that is the clear natural way to sort the class and anyone would need to sort the class would generally want to do the class that way;if however the sorting was an unusual use case of the class or there can be multiple sort orders then a comparator is a better option"

from question  

Sorting ArrayList full of objects by their id using Collections.sort(arrayListName)

Others

Example

Using just the keyfn return a comparable value that matches your requirements is much easier than implementing comparator

from question  

Clojure: sort-by nested map custom comparator

If the type would have to be comparable you couldn t create a treeset with a non-comparable type and a comparator which you can as it is now;one way to fix this while still being type-safe would have been to have two classes one with a comparable type parameter and one with a non-comparable type parameter and no default constructor only the constructor that takes a comparator but i suppose the java devs didn t want to introduce two classes that basically did the same thing though one could easily be implemented as a wrapper around the other

from question  

Why does Java's TreeSet not specify that its type parameter must extend Comparable?

Magine a comparator as a pair of scales a comparator does not measure a comparator but two different objects weights;for this reference you have to implement another interface comparable which is more similar to the built-in object equals

from question  

Why does Comparator.compare take two arguments?

He comparable approach mandates that the keys are always sorted in the same way and would be used for keys which have a canonical ordering like integers where you want to use this ordering;the comparator approach is more flexible since you can use the same key objects for different maps where they are differently ordered and you can use the comparator approach for keys over which you have no control or who don t have a canonical ordering like list trees graphs etc

from question  

Sorting generics in Java

You could also achieve your objective by implements a comparable in the class to sort but you can t as long as string is final so you cannot extends;therefore there is no other way than to implement a comparator which is simpler too

from question  

How to sort LinkedList<String>?

If you can using a comparator is actually much simpler;in fact a comparator gives you a couple of additional options that you cannot have with comparable such as defining alternative sort orders

from question  

Extending an existing object to implement Comparable, in Java

If you later want to be able to sort by something else too then that would not be possible using comparable;so using a separate comparator is more loosely coupled and thus offers more flexibility

from question  

How to sort data sets Java

So first it checks whether comparator is provided if yes treemap uses compare method to compare keys otherwise it uses comparator method of comparable for comparison;treemap uses comparator method of comparable not equals method from object when it tries to put an element into map

from question  

Why Java TreeMap not printing all its inserted entries

But if the actual value comparator is simpler it may improve the code;there s even a parameter-less variant map.entry.comparingbyvalue for the most trivial case that the map values are comparable

from question  

Sort by nested Array size inside nested Map

Back to Home
Data comes from Stack Exchange with CC-BY-SA-4.0