Comparable and Comparator
Wow. Every now and then when you think you know everything something just pops up and suprises you. I’ve recently been studying to take the Sun Certified Programmer certification exam and, even though I thought I knew enough to just go in and take the exam, I decided to go ahead and spend 25 bucks and buy a study guide off of amazon.
Although at first it was a light read, and I find myself skimming sections, I keep coming across interesting and useful features of the language I was unaware of, and sometimes even find myself kicking myself when I realize I came up with half assed solutions that the Java API already solves.
The most interesting features I came across so far were the Comparable and Comparator interfaces. Sure they’re simple, and braindead obvious to use, but I never knew of them before, but I’m glad I do now.
The Comparable interface, as the name suggests, enforces objects to allow themselves to be compared to other objects, which becomes extremely useful in collections such as TreeSet that sort elements as they’re added, or the Collections utility class, which has a lot of sorting features.
Simply put, let’s say I have vehicle objects that I want compared by their vins, so that when they’re sorted in a collection, they’ll be ordered by vin (which, for those who know how to decode a vin, can be quite useful when applying business logic). I’d basically implement the comparable interface and implement a comparable method:
public class Vehicle implements Comparable{
private String vin = "";
...
public int compareTo(Object o) {
return getVin().compareTo(((Vehicle)o).getVin());
}
}
This simply calls the comparesTo method implemented in String and returns the appropriate value. The contract for compares to is quite simple, and is as follows:
x.compareTo(y) returns 0 if x == y
x.compareTo(y) returns -1 if x > y
x.compareTo(y) returns 1 if x < y
(or, specifically, it states it should return zero, a negative int, or a positive int in these cases).
This is great, but what happens if I want to sort by make? Or by model? Or even by year? That’s where the comparator interface comes in. With the comparator inteface you can define specific sort rules to pass in when sorting elements in a collection.
public class MakeComparator<Vehicle> implements Comparator{
public int compare(Vehicle one, Vehicle two) {
return one.getMake().compareTo(two.getMake());
}
}
You can then pass this in with your collection, and it will execute the compator during each compare to sort the collection, such as
Collections.sort(vehicleCollection, new MakeComparator());
Generics comes in very handy here … apparently in java 1.4 you could only pass two Object objects to the compare method, and apparently had to manually test the instances before comparing them. The compareTo method I gave an example of earlier is the Java 1.4 way, and as you can see it’s very error prone and annoying.
I also covered generics last night as well, but that was pretty much a given. I learned a few new things, but I had already discovered most of the features of generics when Java 5 came out, so I won’t go into it here. ![]()
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!







