Source Home >> Java Source 1.6.0 >> java.lang.Comparable V 0.09
  • 001/*
  • 002 * @(#)Comparable.java 1.26 06/04/21
  • 003 *
  • 004 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
  • 005 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  • 006 */
  • 007
  • 008package java.lang;
  • 009import java.util.*;
  • 010
  • 011/**
  • 012 * This interface imposes a total ordering on the objects of each class that
  • 013 * implements it. This ordering is referred to as the class's <i>natural
  • 014 * ordering</i>, and the class's <tt>compareTo</tt> method is referred to as
  • 015 * its <i>natural comparison method</i>.<p>
  • 016 *
  • 017 * Lists (and arrays) of objects that implement this interface can be sorted
  • 018 * automatically by {@link Collections#sort(List) Collections.sort} (and
  • 019 * {@link Arrays#sort(Object[]) Arrays.sort}). Objects that implement this
  • 020 * interface can be used as keys in a {@linkplain SortedMap sorted map} or as
  • 021 * elements in a {@linkplain SortedSet sorted set}, without the need to
  • 022 * specify a {@linkplain Comparator comparator}.<p>
  • 023 *
  • 024 * The natural ordering for a class <tt>C</tt> is said to be <i>consistent
  • 025 * with equals</i> if and only if <tt>e1.compareTo(e2) == 0</tt> has
  • 026 * the same boolean value as <tt>e1.equals(e2)</tt> for every
  • 027 * <tt>e1</tt> and <tt>e2</tt> of class <tt>C</tt>. Note that <tt>null</tt>
  • 028 * is not an instance of any class, and <tt>e.compareTo(null)</tt> should
  • 029 * throw a <tt>NullPointerException</tt> even though <tt>e.equals(null)</tt>
  • 030 * returns <tt>false</tt>.<p>
  • 031 *
  • 032 * It is strongly recommended (though not required) that natural orderings be
  • 033 * consistent with equals. This is so because sorted sets (and sorted maps)
  • 034 * without explicit comparators behave "strangely" when they are used with
  • 035 * elements (or keys) whose natural ordering is inconsistent with equals. In
  • 036 * particular, such a sorted set (or sorted map) violates the general contract
  • 037 * for set (or map), which is defined in terms of the <tt>equals</tt>
  • 038 * method.<p>
  • 039 *
  • 040 * For example, if one adds two keys <tt>a</tt> and <tt>b</tt> such that
  • 041 * <tt>(!a.equals(b) && a.compareTo(b) == 0)</tt> to a sorted
  • 042 * set that does not use an explicit comparator, the second <tt>add</tt>
  • 043 * operation returns false (and the size of the sorted set does not increase)
  • 044 * because <tt>a</tt> and <tt>b</tt> are equivalent from the sorted set's
  • 045 * perspective.<p>
  • 046 *
  • 047 * Virtually all Java core classes that implement <tt>Comparable</tt> have natural
  • 048 * orderings that are consistent with equals. One exception is
  • 049 * <tt>java.math.BigDecimal</tt>, whose natural ordering equates
  • 050 * <tt>BigDecimal</tt> objects with equal values and different precisions
  • 051 * (such as 4.0 and 4.00).<p>
  • 052 *
  • 053 * For the mathematically inclined, the <i>relation</i> that defines
  • 054 * the natural ordering on a given class C is:<pre>
  • 055 * {(x, y) such that x.compareTo(y) <= 0}.
  • 056 * </pre> The <i>quotient</i> for this total order is: <pre>
  • 057 * {(x, y) such that x.compareTo(y) == 0}.
  • 058 * </pre>
  • 059 *
  • 060 * It follows immediately from the contract for <tt>compareTo</tt> that the
  • 061 * quotient is an <i>equivalence relation</i> on <tt>C</tt>, and that the
  • 062 * natural ordering is a <i>total order</i> on <tt>C</tt>. When we say that a
  • 063 * class's natural ordering is <i>consistent with equals</i>, we mean that the
  • 064 * quotient for the natural ordering is the equivalence relation defined by
  • 065 * the class's {@link Object#equals(Object) equals(Object)} method:<pre>
  • 066 * {(x, y) such that x.equals(y)}. </pre><p>
  • 067 *
  • 068 * This interface is a member of the
  • 069 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
  • 070 * Java Collections Framework</a>.
  • 071 *
  • 072 * @param <T> the type of objects that this object may be compared to
  • 073 *
  • 074 * @author Josh Bloch
  • 075 * @version 1.26, 04/21/06
  • 076 * @see java.util.Comparator
  • 077 * @since 1.2
  • 078 */
  • 079
  • 080public interface Comparable<T> {
  • 081 /**
  • 082 * Compares this object with the specified object for order. Returns a
  • 083 * negative integer, zero, or a positive integer as this object is less
  • 084 * than, equal to, or greater than the specified object.
  • 085 *
  • 086 * <p>The implementor must ensure <tt>sgn(x.compareTo(y)) ==
  • 087 * -sgn(y.compareTo(x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
  • 088 * implies that <tt>x.compareTo(y)</tt> must throw an exception iff
  • 089 * <tt>y.compareTo(x)</tt> throws an exception.)
  • 090 *
  • 091 * <p>The implementor must also ensure that the relation is transitive:
  • 092 * <tt>(x.compareTo(y)>0 && y.compareTo(z)>0)</tt> implies
  • 093 * <tt>x.compareTo(z)>0</tt>.
  • 094 *
  • 095 * <p>Finally, the implementor must ensure that <tt>x.compareTo(y)==0</tt>
  • 096 * implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for
  • 097 * all <tt>z</tt>.
  • 098 *
  • 099 * <p>It is strongly recommended, but <i>not</i> strictly required that
  • 100 * <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>. Generally speaking, any
  • 101 * class that implements the <tt>Comparable</tt> interface and violates
  • 102 * this condition should clearly indicate this fact. The recommended
  • 103 * language is "Note: this class has a natural ordering that is
  • 104 * inconsistent with equals."
  • 105 *
  • 106 * <p>In the foregoing description, the notation
  • 107 * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
  • 108 * <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
  • 109 * <tt>0</tt>, or <tt>1</tt> according to whether the value of
  • 110 * <i>expression</i> is negative, zero or positive.
  • 111 *
  • 112 * @param o the object to be compared.
  • 113 * @return a negative integer, zero, or a positive integer as this object
  • 114 * is less than, equal to, or greater than the specified object.
  • 115 *
  • 116 * @throws ClassCastException if the specified object's type prevents it
  • 117 * from being compared to this object.
  • 118 */
  • 119 public int compareTo(T o);
  • 120}

文件:Comparable.java
包名:java.lang
类名:Comparable
继承:
接口: