1 *java.lang.Comparable* *Comparable* This interface imposes a total ordering on t
3 public interface interface Comparable<T>
6 |java.lang.Comparable_Description|
7 |java.lang.Comparable_Fields|
8 |java.lang.Comparable_Constructors|
9 |java.lang.Comparable_Methods|
11 ================================================================================
13 *java.lang.Comparable_Methods*
14 |java.lang.Comparable.compareTo(T)|Compares this object with the specified obje
16 *java.lang.Comparable_Description*
18 This interface imposes a total ordering on the objects of each class that
19 implements it. This ordering is referred to as the class's natural ordering,
20 and the class's compareTo method is referred to as its natural comparison
23 Lists (and arrays) of objects that implement this interface can be sorted
24 automatically by Collections.sort(|java.util.Collections|) (and
25 Arrays.sort(|java.util.Arrays|) ). Objects that implement this interface can be
26 used as keys in a sorted map(|java.util.SortedMap|) or as elements in a sorted
27 set(|java.util.SortedSet|) , without the need to specify a
28 comparator(|java.util.Comparator|) .
30 The natural ordering for a class C is said to be consistent with equals if and
31 only if e1.compareTo(e2) == 0 has the same boolean value as e1.equals(e2) for
32 every e1 and e2 of class C. Note that null is not an instance of any class, and
33 e.compareTo(null) should throw a NullPointerException even though
34 e.equals(null) returns false.
36 It is strongly recommended (though not required) that natural orderings be
37 consistent with equals. This is so because sorted sets (and sorted maps)
38 without explicit comparators behave "strangely" when they are used with
39 elements (or keys) whose natural ordering is inconsistent with equals. In
40 particular, such a sorted set (or sorted map) violates the general contract for
41 set (or map), which is defined in terms of the equals method.
43 For example, if one adds two keys a and b such that (!a.equals(b)
44 a.compareTo(b) == 0) to a sorted set that does not use an explicit comparator,
45 the second add operation returns false (and the size of the sorted set does not
46 increase) because a and b are equivalent from the sorted set's perspective.
48 Virtually all Java core classes that implement Comparable have natural
49 orderings that are consistent with equals. One exception is
50 java.math.BigDecimal, whose natural ordering equates BigDecimal objects with
51 equal values and different precisions (such as 4.0 and 4.00).
53 For the mathematically inclined, the relation that defines the natural ordering
54 on a given class C is:
56 {(x, y) such that x.compareTo(y) <= 0}.
58 The quotient for this total order is:
60 {(x, y) such that x.compareTo(y) == 0}.
62 It follows immediately from the contract for compareTo that the quotient is an
63 equivalence relation on C, and that the natural ordering is a total order on C.
64 When we say that a class's natural ordering is consistent with equals, we mean
65 that the quotient for the natural ordering is the equivalence relation defined
66 by the class's equals(Object)(|java.lang.Object|) method:
68 {(x, y) such that x.equals(y)}.
70 This interface is a member of the <a
71 href="/../technotes/guides/collections/index.html"> Java Collections Framework.
75 *java.lang.Comparable.compareTo(T)*
77 public int compareTo(T o)
79 Compares this object with the specified object for order. Returns a negative
80 integer, zero, or a positive integer as this object is less than, equal to, or
81 greater than the specified object.
83 The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all
84 x and y. (This implies that x.compareTo(y) must throw an exception iff
85 y.compareTo(x) throws an exception.)
87 The implementor must also ensure that the relation is transitive:
88 (x.compareTo(y)>0 and and y.compareTo(z)>0) implies x.compareTo(z)>0.
90 Finally, the implementor must ensure that x.compareTo(y)==0 implies that
91 sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all z.
93 It is strongly recommended, but not strictly required that (x.compareTo(y)==0)
94 == (x.equals(y)). Generally speaking, any class that implements the Comparable
95 interface and violates this condition should clearly indicate this fact. The
96 recommended language is "Note: this class has a natural ordering that is
97 inconsistent with equals."
99 In the foregoing description, the notation sgn(expression) designates the
100 mathematical signum function, which is defined to return one of -1, 0, or 1
101 according to whether the value of expression is negative, zero or positive.
104 o - the object to be compared.
106 Returns: a negative integer, zero, or a positive integer as this object is less than,
107 equal to, or greater than the specified object.