Why is Java Comparable
used? Why would someone implement Comparable
in a class? What is a real life example where you need to implement comparable?
-
3Related: When to use Comparable vs Comparator and Sorting an ArrayList.– BalusCCommented Sep 15, 2010 at 14:08
-
There's a good example here: java67.blogspot.com/2012/10/…– james.garrissCommented Jan 27, 2016 at 12:35
10 Answers
Here is a real life sample. Note that String
also implements Comparable
.
class Author implements Comparable<Author>{
String firstName;
String lastName;
@Override
public int compareTo(Author other){
// compareTo should return < 0 if this is supposed to be
// less than other, > 0 if this is supposed to be greater than
// other and 0 if they are supposed to be equal
int last = this.lastName.compareTo(other.lastName);
return last == 0 ? this.firstName.compareTo(other.firstName) : last;
}
}
later..
/**
* List the authors. Sort them by name so it will look good.
*/
public List<Author> listAuthors(){
List<Author> authors = readAuthorsFromFileOrSomething();
Collections.sort(authors);
return authors;
}
/**
* List unique authors. Sort them by name so it will look good.
*/
public SortedSet<Author> listUniqueAuthors(){
List<Author> authors = readAuthorsFromFileOrSomething();
return new TreeSet<Author>(authors);
}
-
17
-
-
1@AnirbanNag'tintinmj' to sort automatically by the first name in case that the last name is the same.– OddDevCommented Aug 5, 2015 at 7:49
-
Plus one for the good explanation of why compareTo returns an int and what it means. Most helpful. Commented Jan 27, 2016 at 13:19
-
1@user3932000: Right, that's pretty much the point of all interfaces. But note that "other Java methods" includes methods written by the user! In fact I'd claim majority of interfaces are consumed by the user's code. In larger code bases, "yourself" quickly becomes "others" Commented May 2, 2017 at 6:01
Comparable defines a natural ordering. What this means is that you're defining it when one object should be considered "less than" or "greater than".
Suppose you have a bunch of integers and you want to sort them. That's pretty easy, just put them in a sorted collection, right?
TreeSet<Integer> m = new TreeSet<Integer>();
m.add(1);
m.add(3);
m.add(2);
for (Integer i : m)
... // values will be sorted
But now suppose I have some custom object, where sorting makes sense to me, but is undefined. Let's say, I have data representing districts by zipcode with population density, and I want to sort them by density:
public class District {
String zipcode;
Double populationDensity;
}
Now the easiest way to sort them is to define them with a natural ordering by implementing Comparable, which means there's a standard way these objects are defined to be ordered.:
public class District implements Comparable<District>{
String zipcode;
Double populationDensity;
public int compareTo(District other)
{
return populationDensity.compareTo(other.populationDensity);
}
}
Note that you can do the equivalent thing by defining a comparator. The difference is that the comparator defines the ordering logic outside the object. Maybe in a separate process I need to order the same objects by zipcode - in that case the ordering isn't necessarily a property of the object, or differs from the objects natural ordering. You could use an external comparator to define a custom ordering on integers, for example by sorting them by their alphabetical value.
Basically the ordering logic has to exist somewhere. That can be -
in the object itself, if it's naturally comparable (extends Comparable -e.g. integers)
supplied in an external comparator, as in the example above.
-
good example, but it has to be
TreeSet<Integer>
instead ofTreeMap<Integer>
, for the latter does not exist, TreeMaps are always<Key,Value>
-pairs. Btw, a hypotheticalTreeMap<District, Object>
would only work, if District implemented Comparable, right? Still trying to understand this– phil294Commented Jan 31, 2015 at 16:25
Quoted from the javadoc;
This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.
Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator.
Edit: ..and made the important bit bold.
-
4I'd say that the sentence after the one you've bolded is just as (if not more) important. Commented Sep 15, 2010 at 14:12
The fact that a class implements Comparable
means that you can take two objects from that class and compare them. Some classes, like certain collections (sort function in a collection) that keep objects in order rely on them being comparable (in order to sort you need to know which object is the "biggest" and so forth).
Most of the examples above show how to reuse an existing comparable object in the compareTo function. If you would like to implement your own compareTo when you want to compare two objects of the same class, say an AirlineTicket object that you would like to sort by price(less is ranked first), followed by number of stopover (again, less is ranked first), you would do the following:
class AirlineTicket implements Comparable<Cost>
{
public double cost;
public int stopovers;
public AirlineTicket(double cost, int stopovers)
{
this.cost = cost; this.stopovers = stopovers ;
}
public int compareTo(Cost o)
{
if(this.cost != o.cost)
return Double.compare(this.cost, o.cost); //sorting in ascending order.
if(this.stopovers != o.stopovers)
return this.stopovers - o.stopovers; //again, ascending but swap the two if you want descending
return 0;
}
}
-
Class
Cost
has an attributecost
asAirlineTicket
? And the same for the attributestopovers
(in the two classes)? It is very strange... What is the implementation of Cost ? Commented Apr 28, 2021 at 22:12
An easy way to implement multiple field comparisons is with Guava's ComparisonChain - then you can say
public int compareTo(Foo that) {
return ComparisonChain.start()
.compare(lastName, that.lastName)
.compare(firstName, that.firstName)
.compare(zipCode, that.zipCode)
.result();
}
instead of
public int compareTo(Person other) {
int cmp = lastName.compareTo(other.lastName);
if (cmp != 0) {
return cmp;
}
cmp = firstName.compareTo(other.firstName);
if (cmp != 0) {
return cmp;
}
return Integer.compare(zipCode, other.zipCode);
}
}
Comparable is used to compare instances of your class. We can compare instances from many ways that is why we need to implement a method compareTo
in order to know how (attributes) we want to compare instances.
Dog
class:
package test;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Dog d1 = new Dog("brutus");
Dog d2 = new Dog("medor");
Dog d3 = new Dog("ara");
Dog[] dogs = new Dog[3];
dogs[0] = d1;
dogs[1] = d2;
dogs[2] = d3;
for (int i = 0; i < 3; i++) {
System.out.println(dogs[i].getName());
}
/**
* Output:
* brutus
* medor
* ara
*/
Arrays.sort(dogs, Dog.NameComparator);
for (int i = 0; i < 3; i++) {
System.out.println(dogs[i].getName());
}
/**
* Output:
* ara
* medor
* brutus
*/
}
}
Main
class:
package test;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Dog d1 = new Dog("brutus");
Dog d2 = new Dog("medor");
Dog d3 = new Dog("ara");
Dog[] dogs = new Dog[3];
dogs[0] = d1;
dogs[1] = d2;
dogs[2] = d3;
for (int i = 0; i < 3; i++) {
System.out.println(dogs[i].getName());
}
/**
* Output:
* brutus
* medor
* ara
*/
Arrays.sort(dogs, Dog.NameComparator);
for (int i = 0; i < 3; i++) {
System.out.println(dogs[i].getName());
}
/**
* Output:
* ara
* medor
* brutus
*/
}
}
Here is a good example how to use comparable in Java:
http://www.onjava.com/pub/a/onjava/2003/03/12/java_comp.html?page=2
-
You just copied your main class codebase instead of the Dog class. Where is the code for Dog.NameComparator Commented Nov 16, 2020 at 9:04
For example when you want to have a sorted collection or map
-
What Fernando means is: if you store "things" that implement Comparable in a sorted container class, the sorted container class can automatically order those "things". Commented Mar 30, 2012 at 18:51
OK, but why not just define a compareTo()
method without implementing comparable interface.
For example a class City
defined by its name
and temperature
and
public int compareTo(City theOther)
{
if (this.temperature < theOther.temperature)
return -1;
else if (this.temperature > theOther.temperature)
return 1;
else
return 0;
}
-
This does not work. Without implementing comparable - I get classcast exception Commented Mar 19, 2019 at 14:48
When you implement Comparable
interface, you need to implement method compareTo()
. You need it to compare objects, in order to use, for example, sorting method of ArrayList
class. You need a way to compare your objects to be able to sort them. So you need a custom compareTo()
method in your class so you can use it with the ArrayList
sort method. The compareTo()
method returns -1,0,1.
I have just read an according chapter in Java Head 2.0, I'm still learning.