Implementing Comparable and understanding compareTo
Implementing Comparable
Implementing Comparable allows:
- Calling Collections.sort and Collections.binarySearch.
- Calling Arrays.sort and Arrays.binarySearch.
- Using objects as keys in a TreeMap.
- Using objects as elements in a TreeSet.
Understanding compareTo
The compareTo() method is the sole member of Comparable interface. It provides a means of fully ordering objects. For a concrete comparable implementation class to work well, the compareTo() implementation needs to satisfy the certain conditions.
- Anti Commutation : x.compareTo(y) is the opposite sign of y.compareTo(x)
- Exception Symmetry : x.compareTo(y) throws exactly the same exceptions as y.compareTo(x)
- Transitivity :
- if x.compareTo(y) > 0 and y.compareTo(z) > 0, then x.compareTo(z) > 0 (and same for less than)
- if x.compareTo(y)==0, then x.compareTo(z) has the same sign as y.compareTo(z)
- consistency with equals : It is highly recommended, but not required : x.compareTo(y) == 0, if and only if x.equals(y) ; consistency with equals is required for ensuring sorted collections (such as TreeSet) are well-behaved.
Things to remember while implementing compareTo()
Compare the various types of fields as follows :
- Numeric primitive : use < and >. There is an exception to this rule: float and double primitives should be compared using Float.compare(float, float) and Double.compare(double, double). This avoids problems associated with special border values.
- Boolean primitive : use tests of the form (x && !y)
- Object : use compareTo. (Note that possibly-null fields present a problem : while x.equals(null) returns false, x.compareTo(null) will always throw a NullPointerException)
- Type-safe enumeration : use compareTo, like any Object
- collection or array : Comparable does not seem to be intended for these kinds of fields. For example, List, Map and Set do not implement Comparable. As well, some collections have no definite order of iteration, so doing an element-by-element comparison cannot be meaningful in those cases.
Comparable implementations in JDK
All primitive wrapper classes like Integer, Long, Float, Double, Boolean and many more implement Comparable.
Hot tips
- One can greatly increase the performance of compareTo by comparing first on items which are most likely to differ.
- Avoid instanceof in methods that override or implement Object.equals(), Comparable.compareTo()
- If the task is to perform a sort of items which are stored in a relational database, then it is usually much preferred to let the database perform the sort using the ORDER BY clause, rather than in code.
- An alternative to implementing Comparable is passing Comparator objects as parameters. Be aware that if a Comparator compares only one of several significant fields, then the Comparator is very likely not synchronized with equals.
- When a class extends a concrete Comparable class and adds a significant field, a correct implementation of compareTo cannot be constructed. The only alternative is to use composition instead of inheritance. (A similar situation holds true for equals. See Effective Java for more information.)
Similar Articles
Linked data based integration |
New Date and Time API in Java8 |
Re-platformig this blog to Wintersmith |
Improving code quality in agile teams |
Java7 updates |