Usually while thinking of real time examples for a code solution, one often ends up comparing two objects. e.g. listing the companies in IT in the reverse order of their turnovers. This needs some comparison between 2 instances of object say TurnOver. Enough for the intro, the title tells you where exactly I am heading.
Advantages of implementing comparable
- Implement for natural comparing
- TreeSet uses compareTo for comparing and sorting a set
- HashSet uses equals to avoid redundant keys
- Arrays.sort(a); can be used where ‘a’ is an array of comaprable object
General Contract of compareTo method
To cut the story short:
- Must return 1, -1 or 0 only
- If something acts as a constraint in comparing the two objects, throw a ClassCastException
- Throws a NullPointerException
- The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception if and only if y.compareTo(x) throws an exception)
- The implementor must also ensure that the relation is transitive: (x.compareTo(y) > 0 && y.compareTo(z) > 0) implies x.compareTo(z) > 0
- Finally, the implementor must ensure that x.compareTo(y) == 0 implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all z
- It is strongly recommended, but not strictly required, that (x.compareTo(y) == 0) == (x.equals(y))
Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is “Note: This class has a natural ordering that is inconsistent with equals.”
Example where equals and compareTo behave differently
Consider 2 BigDecimal objects instantiated with String values “1.00” and “1.0”.
Behavior of equals method of BigDecimal -What would the following code print?
BigDecimal bd1 = new BigDecimal("1.00"); BigDecimal bd2 = new BigDecimal("1.0"); System.out.println(bd1.equals(bd2));
This code prints a big fat false. While..
Behavior of compareTo method of BigDecimal -What does this piece of code print?
BigDecimal bd1 = new BigDecimal("1.00"); BigDecimal bd2 = new BigDecimal("1.0"); System.out.println(bd1.compareTo(bd2));
Wait for it ……..
Yes, it returns 0. I.e. there may be examples where the behavior of equals and compareTo may be different. But, such cases must be avoided.
Involve all the important fields in compareTo
Try and include all the relevant fields in compareTo. There may be times when one needs to sort a list on the basis of multiple fields. e.g. if the phone numbers are to be sorted according to the state code and area code, giving preference to the state code. Then a class PhoneNumber’s logic will use both state code and area code in compareTo logic.