Path: blob/master/src/java.base/share/classes/java/lang/Comparable.java
41152 views
/*1* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.lang;26import java.util.*;2728/**29* This interface imposes a total ordering on the objects of each class that30* implements it. This ordering is referred to as the class's <i>natural31* ordering</i>, and the class's {@code compareTo} method is referred to as32* its <i>natural comparison method</i>.<p>33*34* Lists (and arrays) of objects that implement this interface can be sorted35* automatically by {@link Collections#sort(List) Collections.sort} (and36* {@link Arrays#sort(Object[]) Arrays.sort}). Objects that implement this37* interface can be used as keys in a {@linkplain SortedMap sorted map} or as38* elements in a {@linkplain SortedSet sorted set}, without the need to39* specify a {@linkplain Comparator comparator}.<p>40*41* The natural ordering for a class {@code C} is said to be <i>consistent42* with equals</i> if and only if {@code e1.compareTo(e2) == 0} has43* the same boolean value as {@code e1.equals(e2)} for every44* {@code e1} and {@code e2} of class {@code C}. Note that {@code null}45* is not an instance of any class, and {@code e.compareTo(null)} should46* throw a {@code NullPointerException} even though {@code e.equals(null)}47* returns {@code false}.<p>48*49* It is strongly recommended (though not required) that natural orderings be50* consistent with equals. This is so because sorted sets (and sorted maps)51* without explicit comparators behave "strangely" when they are used with52* elements (or keys) whose natural ordering is inconsistent with equals. In53* particular, such a sorted set (or sorted map) violates the general contract54* for set (or map), which is defined in terms of the {@code equals}55* method.<p>56*57* For example, if one adds two keys {@code a} and {@code b} such that58* {@code (!a.equals(b) && a.compareTo(b) == 0)} to a sorted59* set that does not use an explicit comparator, the second {@code add}60* operation returns false (and the size of the sorted set does not increase)61* because {@code a} and {@code b} are equivalent from the sorted set's62* perspective.<p>63*64* Virtually all Java core classes that implement {@code Comparable}65* have natural orderings that are consistent with equals. One66* exception is {@link java.math.BigDecimal}, whose {@linkplain67* java.math.BigDecimal#compareTo natural ordering} equates {@code68* BigDecimal} objects with equal numerical values and different69* representations (such as 4.0 and 4.00). For {@link70* java.math.BigDecimal#equals BigDecimal.equals()} to return true,71* the representation and numerical value of the two {@code72* BigDecimal} objects must be the same.<p>73*74* For the mathematically inclined, the <i>relation</i> that defines75* the natural ordering on a given class C is:<pre>{@code76* {(x, y) such that x.compareTo(y) <= 0}.77* }</pre> The <i>quotient</i> for this total order is: <pre>{@code78* {(x, y) such that x.compareTo(y) == 0}.79* }</pre>80*81* It follows immediately from the contract for {@code compareTo} that the82* quotient is an <i>equivalence relation</i> on {@code C}, and that the83* natural ordering is a <i>total order</i> on {@code C}. When we say that a84* class's natural ordering is <i>consistent with equals</i>, we mean that the85* quotient for the natural ordering is the equivalence relation defined by86* the class's {@link Object#equals(Object) equals(Object)} method:<pre>87* {(x, y) such that x.equals(y)}. </pre><p>88*89* In other words, when a class's natural ordering is consistent with90* equals, the equivalence classes defined by the equivalence relation91* of the {@code equals} method and the equivalence classes defined by92* the quotient of the {@code compareTo} method are the same.93*94* <p>This interface is a member of the95* <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">96* Java Collections Framework</a>.97*98* @param <T> the type of objects that this object may be compared to99*100* @author Josh Bloch101* @see java.util.Comparator102* @since 1.2103*/104public interface Comparable<T> {105/**106* Compares this object with the specified object for order. Returns a107* negative integer, zero, or a positive integer as this object is less108* than, equal to, or greater than the specified object.109*110* <p>The implementor must ensure {@link Integer#signum111* signum}{@code (x.compareTo(y)) == -signum(y.compareTo(x))} for112* all {@code x} and {@code y}. (This implies that {@code113* x.compareTo(y)} must throw an exception if and only if {@code114* y.compareTo(x)} throws an exception.)115*116* <p>The implementor must also ensure that the relation is transitive:117* {@code (x.compareTo(y) > 0 && y.compareTo(z) > 0)} implies118* {@code x.compareTo(z) > 0}.119*120* <p>Finally, the implementor must ensure that {@code121* x.compareTo(y)==0} implies that {@code signum(x.compareTo(z))122* == signum(y.compareTo(z))}, for all {@code z}.123*124* @apiNote125* It is strongly recommended, but <i>not</i> strictly required that126* {@code (x.compareTo(y)==0) == (x.equals(y))}. Generally speaking, any127* class that implements the {@code Comparable} interface and violates128* this condition should clearly indicate this fact. The recommended129* language is "Note: this class has a natural ordering that is130* inconsistent with equals."131*132* @param o the object to be compared.133* @return a negative integer, zero, or a positive integer as this object134* is less than, equal to, or greater than the specified object.135*136* @throws NullPointerException if the specified object is null137* @throws ClassCastException if the specified object's type prevents it138* from being compared to this object.139*/140public int compareTo(T o);141}142143144