This is the 27th day of my participation in the August More Text Challenge
preface
The compareTo method is not defined in the Object class, but is the only method in the Comparable interface that allows not only simple equality comparisons, but also sequential comparisons. Once the Comparable interface is implemented, it is naturally ordered. You can collaborate with many generic methods and the collection implementation classes that depend on the interface.
The instance
public class User implements Comparable<User>{...@Override
public int compareTo(User o) {
return 0; }}Copy the code
Returns an int, where a negative number means less than, 0 means equal, and a positive number means greater than
A specification to implement CompareTo methods
Be consistent with equals
An important convention for using the compareTo method is that in general the compareTo method imposes the same equality tests as the Equals method. If they are inconsistent, the equals method is used to test the equality of ordered collections, while the compareTo method is used to test the equality of ordered collections. If they are inconsistent, the results can be disastrous.
Compares the object with the specified object, returning the corresponding number
The compareTo method is preceded by the rules that you need to follow to implement the compareTo method
Returns a negative integer, zero integer, or positive integer if the object is less than, equal to, or greater than the specified object, and throws a ClassCastException if the object cannot be compared to the specified object because of its type. In the following illustration, the symbol SGN (expression) represents the mathematical signum function, which returns -1, 0, and 1, respectively, depending on the value of expression.
one
You must ensure that all x and y satisfy SGN (x.compareTo(y)) == -sgn(y.compareTo(x)). This also implies that x.compareTo(y) throws an exception if and only if y.compareTo(x) does.
two
You must ensure that the comparison is transitive :(x.compareTo(y) > 0 && y.compareTo(z) > 0) implies that x.compareTo(z) > 0 is also true. Corresponds to equals using transitivity in the specification.
three
We must ensure that x.compareTo(y) == 0 implies that all z satisfy SGN (x.compareTo(z)) == SGN (y.compareTo(z)).
four
It is strongly recommended (x.compareTo(y) == 0) == (x.equals(y)), but this is not absolutely necessary. In general, any class that implements the Comparable interface that violates this condition should be explicitly stated. It is recommended to use the following statement: “Note that this class has built-in sorting capabilities, but is inconsistent with equals.”
For example, compareTo and Equals for BigDecimal are different
BigDecimal t = new BigDecimal("1.0");
BigDecimal tt = new BigDecimal("1.00");
HashSet hs = new HashSet();
hs.add(t);
hs.add(tt);
TreeSet ts = new TreeSet();
ts.add(t);
ts.add(tt);
Copy the code
For BigDecimal’s equals, 1.0 and 1.00 are not the same value
For BigDecimal’s compareTo, 1.0 and 1.00 are the same number.
If a class has multiple key fields, the order in which they are compared is critical. Start with the most critical fields and then compare all the important fields. As long as the comparison results in a non-zero result (0 means equal), the entire comparison operation ends and the result is returned.
conclusion
The above specifications must be followed when implementing a Comparable interface, otherwise there could be serious consequences without knowing it…