1 *java.lang.Comparable* *Comparable* This interface imposes a total ordering on t
3 public interface interface Comparable
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 (and Arrays.sort). Objects that implement
25 this interface can be used as keys in a sorted map or elements in a sorted set,
26 without the need to specify a comparator.
28 The natural ordering for a class C is said to be consistent with equals if and
29 only if (e1.compareTo((Object)e2) == 0) has the same boolean value as
30 e1.equals((Object)e2) for every e1 and e2 of class C. Note that null is not an
31 instance of any class, and e.compareTo(null) should throw a
32 NullPointerException even though e.equals(null) returns false.
34 It is strongly recommended (though not required) that natural orderings be
35 consistent with equals. This is so because sorted sets (and sorted maps)
36 without explicit comparators behave "strangely" when they are used with
37 elements (or keys) whose natural ordering is inconsistent with equals. In
38 particular, such a sorted set (or sorted map) violates the general contract for
39 set (or map), which is defined in terms of the equals method.
41 For example, if one adds two keys a and b such that (!a.equals((Object)b)
42 a.compareTo((Object)b) == 0) to a sorted set that does not use an explicit
43 comparator, the second add operation returns false (and the size of the sorted
44 set does not increase) because a and b are equivalent from the sorted set's
47 Virtually all Java core classes that implement comparable have natural
48 orderings that are consistent with equals. One exception is
49 java.math.BigDecimal, whose natural ordering equates BigDecimal objects with
50 equal values and different precisions (such as 4.0 and 4.00).
52 For the mathematically inclined, the relation that defines the natural ordering
53 on a given class C is:
55 {(x, y) such that x.compareTo((Object)y) <= 0}.
57 The quotient for this total order is:
59 {(x, y) such that x.compareTo((Object)y) == 0}.
61 It follows immediately from the contract for compareTo that the quotient is an
62 equivalence relation on C, and that the natural ordering is a total order on C.
63 When we say that a class's natural ordering is consistent with equals, we mean
64 that the quotient for the natural ordering is the equivalence relation defined
65 by the class's equals(Object) method:
67 {(x, y) such that x.equals((Object)y)}.
69 This interface is a member of the <a href="/../guide/collections/index.html">
70 Java Collections Framework.
73 *java.lang.Comparable.compareTo(T)*
75 public int compareTo(java.lang.Object o)
77 Compares this object with the specified object for order. Returns a negative
78 integer, zero, or a positive integer as this object is less than, equal to, or
79 greater than the specified object.
81 In the foregoing description, the notation sgn(expression) designates the
82 mathematical signum function, which is defined to return one of -1, 0, or 1
83 according to whether the value of expression is negative, zero or positive.
85 The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all
86 x and y. (This implies that x.compareTo(y) must throw an exception iff
87 y.compareTo(x) throws an exception.)
89 The implementor must also ensure that the relation is transitive:
90 (x.compareTo(y)>0 and and y.compareTo(z)>0) implies x.compareTo(z)>0.
92 Finally, the implementer must ensure that x.compareTo(y)==0 implies that
93 sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all z.
95 It is strongly recommended, but not strictly required that (x.compareTo(y)==0)
96 == (x.equals(y)). Generally speaking, any class that implements the Comparable
97 interface and violates this condition should clearly indicate this fact. The
98 recommended language is "Note: this class has a natural ordering that is
99 inconsistent with equals."
101 o - the Object to be compared.
103 Returns: a negative integer, zero, or a positive integer as this object is less than,
104 equal to, or greater than the specified object.