<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc (build 1.4.2-rc) on Fri Jun 13 00:14:17 PDT 2003 -->
<TITLE>
Comparable (Java 2 Platform SE v1.4.2)
</TITLE>
<META NAME="keywords" CONTENT="java.lang.Comparable interface">
<META NAME="keywords" CONTENT="compareTo()">
<LINK REL ="stylesheet" TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style">
<SCRIPT type="text/javascript">
function windowTitle()
{
parent.document.title="Comparable (Java 2 Platform SE v1.4.2)";
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="white" onload="windowTitle();">
<!-- ========= START OF TOP NAVBAR ======= -->
<A NAME="navbar_top"><!-- --></A>
<A HREF="#skip-navbar_top" title="Skip navigation links"></A>
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY="">
<TR>
<TD COLSPAN=3 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
<A NAME="navbar_top_firstrow"><!-- --></A>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">
<TR ALIGN="center" VALIGN="top">
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A> </TD>
<TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="class-use/Comparable.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../index-files/index-1.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD>
</TR>
</TABLE>
</TD>
<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
<b>Java<sup><font size=-2>TM</font></sup> 2 Platform<br>Std. Ed. v1.4.2</b></EM>
</TD>
</TR>
<TR>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
<A HREF="../../java/lang/Cloneable.html" title="interface in java.lang"><B>PREV CLASS</B></A>
<A HREF="../../java/lang/Runnable.html" title="interface in java.lang"><B>NEXT CLASS</B></A></FONT></TD>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
<A HREF="../../index.html" target="_top"><B>FRAMES</B></A>
<A HREF="Comparable.html" target="_top"><B>NO FRAMES</B></A>
<SCRIPT type="text/javascript">
<!--
if(window==top) {
document.writeln('<A HREF="../../allclasses-noframe.html"><B>All Classes</B></A>');
}
//-->
</SCRIPT>
<NOSCRIPT>
<A HREF="../../allclasses-noframe.html"><B>All Classes</B></A>
</NOSCRIPT>
</FONT></TD>
</TR>
<TR>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
SUMMARY: NESTED | FIELD | CONSTR | <A HREF="#method_summary">METHOD</A></FONT></TD>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
DETAIL: FIELD | CONSTR | <A HREF="#method_detail">METHOD</A></FONT></TD>
</TR>
</TABLE>
<A NAME="skip-navbar_top"></A>
<!-- ========= END OF TOP NAVBAR ========= -->
<HR>
<!-- ======== START OF CLASS DATA ======== -->
<H2>
<FONT SIZE="-1">
java.lang</FONT>
<BR>
Interface Comparable</H2>
<DL>
<DT><B>All Known Implementing Classes:</B> <DD><A HREF="../../java/math/BigDecimal.html" title="class in java.math">BigDecimal</A>, <A HREF="../../java/math/BigInteger.html" title="class in java.math">BigInteger</A>, <A HREF="../../java/lang/Byte.html" title="class in java.lang">Byte</A>, <A HREF="../../java/nio/ByteBuffer.html" title="class in java.nio">ByteBuffer</A>, <A HREF="../../java/lang/Character.html" title="class in java.lang">Character</A>, <A HREF="../../java/nio/CharBuffer.html" title="class in java.nio">CharBuffer</A>, <A HREF="../../java/nio/charset/Charset.html" title="class in java.nio.charset">Charset</A>, <A HREF="../../java/text/CollationKey.html" title="class in java.text">CollationKey</A>, <A HREF="../../java/util/Date.html" title="class in java.util">Date</A>, <A HREF="../../java/lang/Double.html" title="class in java.lang">Double</A>, <A HREF="../../java/nio/DoubleBuffer.html" title="class in java.nio">DoubleBuffer</A>, <A HREF="../../java/io/File.html" title="class in java.io">File</A>, <A HREF="../../java/lang/Float.html" title="class in java.lang">Float</A>, <A HREF="../../java/nio/FloatBuffer.html" title="class in java.nio">FloatBuffer</A>, <A HREF="../../java/nio/IntBuffer.html" title="class in java.nio">IntBuffer</A>, <A HREF="../../java/lang/Integer.html" title="class in java.lang">Integer</A>, <A HREF="../../java/lang/Long.html" title="class in java.lang">Long</A>, <A HREF="../../java/nio/LongBuffer.html" title="class in java.nio">LongBuffer</A>, <A HREF="../../java/io/ObjectStreamField.html" title="class in java.io">ObjectStreamField</A>, <A HREF="../../java/lang/Short.html" title="class in java.lang">Short</A>, <A HREF="../../java/nio/ShortBuffer.html" title="class in java.nio">ShortBuffer</A>, <A HREF="../../java/lang/String.html" title="class in java.lang">String</A>, <A HREF="../../java/net/URI.html" title="class in java.net">URI</A></DD>
</DL>
<HR>
<DL>
<DT>public interface <B>Comparable</B></DL>
<P>
This interface imposes a total ordering on the objects of each class that
implements it. This ordering is referred to as the class's <i>natural
ordering</i>, and the class's <tt>compareTo</tt> method is referred to as
its <i>natural comparison method</i>.
实现该接口的类的对象必须进行完整的排序,这种排序称作类的自然排序,此类的compareTo方法称为此类的自然比较方法。<p>
Lists (and arrays) of objects that implement this interface can be sorted
automatically by <tt>Collections.sort</tt> (and <tt>Arrays.sort</tt>).
Objects that implement this interface can be used as keys in a sorted map
or elements in a sorted set, without the need to specify a comparator.
实现该接口的列表(或数组)对象可以使用Collections.sort(Arrays.sort)。
实现该接口的对象可以作为排序map中的关键字或排序set中的元素,不需要指定比较器。<p>
The natural ordering for a class <tt>C</tt> is said to be <i>consistent
with equals</i> if and only if <tt>(e1.compareTo((Object)e2) == 0)</tt> has
the same boolean value as <tt>e1.equals((Object)e2)</tt> for every
<tt>e1</tt> and <tt>e2</tt> of class <tt>C</tt>. Note that <tt>null</tt>
is not an instance of any class, and <tt>e.compareTo(null)</tt> should
throw a <tt>NullPointerException</tt> even though <tt>e.equals(null)</tt>
returns <tt>false</tt>.
类的自然排序和equals的一致性表现在:只有当e1.compareTo((Object)e2) == 0,
e1.equals((Object)e2)的布尔值也相同。注意null不是任何类的实例,e.compareTo(null)应当抛出NullPointerException
尽管e.equals(null)返回false。<p>
It is strongly recommended (though not required) that natural orderings be
consistent with equals. This is so because sorted sets (and sorted maps)
without explicit comparators behave "strangely" when they are used with
elements (or keys) whose natural ordering is inconsistent with equals. In
particular, such a sorted set (or sorted map) violates the general contract
for set (or map), which is defined in terms of the <tt>equals</tt>
method.
强烈推荐(并非必须)自然排序和equals一致。这是因为排序set(排序map)不带显式比较器,当它们
被用作元素(或者关键字)时表现“奇怪”,自然排序和equals不一致。尤其是排序set和(排序map)违反了
set和map的一般约定,被按照equals的方法进行定义。
<p>
For example, if one adds two keys <tt>a</tt> and <tt>b</tt> such that
<tt>(!a.equals((Object)b) && a.compareTo((Object)b) == 0)</tt> to a sorted
set that does not use an explicit comparator, the second <tt>add</tt>
operation returns false (and the size of the sorted set does not increase)
because <tt>a</tt> and <tt>b</tt> are equivalent from the sorted set's
perspective.
例如,如果增加了两个关键字:a和b,对于不使用显示比较器的排序set而言,(!a.equals((Object)b) && a.compareTo((Object)b) == 0),
第二次add操作会返回false(排序set的大小不增加),因为从排序set的角度而言,a和b是不相等的。<p>
Virtually all Java core classes that implement comparable have natural
orderings that are consistent with equals. One exception is
<tt>java.math.BigDecimal</tt>, whose natural ordering equates
<tt>BigDecimal</tt> objects with equal values and different precisions
(such as 4.0 and 4.00).
实际上所有实现Comparable的Java核心类的自然排序都和equals一致。java.math.BigDecimal是个例外,
有相等值和不同精度(例如4.0和4.00)的自然排序相等。<p>
For the mathematically inclined, the <i>relation</i> that defines
the natural ordering on a given class C is:
为了向数学上倾斜,在给定Comparale类定义自然排序的关系为:<pre>
{(x, y) such that x.compareTo((Object)y) <= 0}.
</pre> The <i>quotient</i> for this total order is:
全排序的商为:<pre>
{(x, y) such that x.compareTo((Object)y) == 0}.
</pre>
It follows immediately from the contract for <tt>compareTo</tt> that the
quotient is an <i>equivalence relation</i> on <tt>C</tt>, and that the
natural ordering is a <i>total order</i> on <tt>C</tt>. When we say that a
class's natural ordering is <i>consistent with equals</i>, we mean that the
quotient for the natural ordering is the equivalence relation defined by
the class's <tt>equals(Object)</tt> method:
在Comparable类compareTo中约定,商为等式,自然排序是全排序。当我们说一个类的自然排序和equals一致时
自然排序的商是个由类equals(Object)方法中定义的等式:
<pre>
{(x, y) such that x.equals((Object)y)}.
</pre><p>
This interface is a member of the
<a href="../../../guide/collections/index.html">
Java Collections Framework</a>.
该接口是Java集合框架的一部分。
<P>
<P>
<DL>
<DT><B>Since:</B></DT>
<DD>1.2</DD>
<DT><B>See Also:</B><DD><A HREF="../../java/util/Comparator.html" title="interface in java.util"><CODE>Comparator</CODE></A>,
<A HREF="../../java/util/Collections.html#sort(java.util.List)"><CODE>Collections.sort(java.util.List)</CODE></A>,
<A HREF="../../java/util/Arrays.html#sort(java.lang.Object[])"><CODE>Arrays.sort(Object[])</CODE></A>,
<A HREF="../../java/util/SortedSet.html" title="interface in java.util"><CODE>SortedSet</CODE></A>,
<A HREF="../../java/util/SortedMap.html" title="interface in java.util"><CODE>SortedMap</CODE></A>,
<A HREF="../../java/util/TreeSet.html" title="class in java.util"><CODE>TreeSet</CODE></A>,
<A HREF="../../java/util/TreeMap.html" title="class in java.util"><CODE>TreeMap</CODE></A></DL>
<HR>
<P>
<!-- ======== NESTED CLASS SUMMARY ======== -->
<!-- =========== FIELD SUMMARY =========== -->
<!-- ======== CONSTRUCTOR SUMMARY ======== -->
<!-- ========== METHOD SUMMARY =========== -->
<A NAME="method_summary"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TD COLSPAN=2><FONT SIZE="+2">
<B>Method Summary</B></FONT></TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE> int</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../java/lang/Comparable.html#compareTo(java.lang.Object)">compareTo</A></B>(<A HREF="../../java/lang/Object.html" title="class in java.lang">Object</A> o)</CODE>
<BR>
Compares this object with the specified object for order.
将该对象与指定对象按顺序进行比较。</TD>
</TR>
</TABLE>
<P>
<!-- ============ FIELD DETAIL =========== -->
<!-- ========= CONSTRUCTOR DETAIL ======== -->
<!-- ============ METHOD DETAIL ========== -->
<A NAME="method_detail"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TD COLSPAN=1><FONT SIZE="+2">
<B>Method Detail</B></FONT></TD>
</TR>
</TABLE>
<A NAME="compareTo(java.lang.Object)"><!-- --></A><H3>
compareTo</H3>
<PRE>
public int <B>compareTo</B>(<A HREF="../../java/lang/Object.html" title="class in java.lang">Object</A> o)</PRE>
<DL>
<DD>Compares this object with the specified object for order. Returns a
negative integer, zero, or a positive integer as this object is less
than, equal to, or greater than the specified object.
将该对象与指定对象按顺序进行比较。如果该对象小于指定对象返回负数,等于返回0,大于返回正数。<p>
In the foregoing description, the notation
<tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
<i>signum</i> function, which is defined to return one of <tt>-1</tt>,
<tt>0</tt>, or <tt>1</tt> according to whether the value of <i>expression</i>
is negative, zero or positive.
The implementor must ensure <tt>sgn(x.compareTo(y)) ==
-sgn(y.compareTo(x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
implies that <tt>x.compareTo(y)</tt> must throw an exception iff
<tt>y.compareTo(x)</tt> throws an exception.)
在先前的叙述中,符号sgn(表达式)表示数学上的signum函数,被定义为:
表达式的值为负,返回-1,为0,返回0,为正,返回1。实现类必须确保所有的x、y都有
sgn(x.compareTo(y))=-sgn(y.compareTo(x))(这意味着,如果y.compareTo(x)抛出一个异常,x.compareTo(y)也要抛出一个异常)。
<p>
The implementor must also ensure that the relation is transitive:
<tt>(x.compareTo(y)>0 && y.compareTo(z)>0)</tt> implies
<tt>x.compareTo(z)>0</tt>.
实现类还必须确保该关系是可传递的(x.compareTo(y)>0 && y.compareTo(z)>0)意味着
x.compareTo(z)>0<p>
Finally, the implementer must ensure that <tt>x.compareTo(y)==0</tt>
implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for
all <tt>z</tt>.
最后,实现类还必须确保x.compareTo(y)==0意味着对于所有的z都有sgn(x.compareTo(z)) == sgn(y.compareTo(z))。<p>
It is strongly recommended, but <i>not</i> strictly required that
<tt>(x.compareTo(y)==0) == (x.equals(y))</tt>. Generally speaking, any
class that implements the <tt>Comparable</tt> interface and violates
this condition should clearly indicate this fact. The recommended
language is "Note: this class has a natural ordering that is
inconsistent with equals."
(x.compareTo(y)==0) == (x.equals(y))是种被强烈推荐的做法,但并非是强制执行。
一般而言,任何实现Comparable接口的类而违反该条件的应当明确提出。推荐如此阐述:
“注意:该类的自然排序和equals不一致。”
<P>
<DD><DL>
<DT><B>Parameters:</B><DD><CODE>o</CODE> - the Object to be compared.被比较的对象。
<DT><B>Returns:</B><DD>a negative integer, zero, or a positive integer as this object
is less than, equal to, or greater than the specified object.
该对象小于指定对象返回负数,等于返回0,大于返回正数。
<DT><B>Throws:</B>
<DD><CODE><A HREF="../../java/lang/ClassCastException.html" title="class in java.lang">ClassCastException</A></CODE> - if the specified object's type prevents it
from being compared to this Object.如果指定的对象类型不允许它和该对象进行比较时抛出。</DL>
</DD>
</DL>
<!-- ========= END OF CLASS DATA ========= -->
<HR>
<!-- ======= START OF BOTTOM NAVBAR ====== -->
<A NAME="navbar_bottom"><!-- --></A>
<A HREF="#skip-navbar_bottom" title="Skip navigation links"></A>
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY="">
<TR>
<TD COLSPAN=3 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
<A NAME="navbar_bottom_firstrow"><!-- --></A>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">
<TR ALIGN="center" VALIGN="top">
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A> </TD>
<TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="class-use/Comparable.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../index-files/index-1.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD>
</TR>
</TABLE>
</TD>
<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
<b>Java<sup><font size=-2>TM</font></sup> 2 Platform<br>Std. Ed. v1.4.2</b></EM>
</TD>
</TR>
<TR>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
<A HREF="../../java/lang/Cloneable.html" title="interface in java.lang"><B>PREV CLASS</B></A>
<A HREF="../../java/lang/Runnable.html" title="interface in java.lang"><B>NEXT CLASS</B></A></FONT></TD>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
<A HREF="../../index.html" target="_top"><B>FRAMES</B></A>
<A HREF="Comparable.html" target="_top"><B>NO FRAMES</B></A>
<SCRIPT type="text/javascript">
<!--
if(window==top) {
document.writeln('<A HREF="../../allclasses-noframe.html"><B>All Classes</B></A>');
}
//-->
</SCRIPT>
<NOSCRIPT>
<A HREF="../../allclasses-noframe.html"><B>All Classes</B></A>
</NOSCRIPT>
</FONT></TD>
</TR>
<TR>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
SUMMARY: NESTED | FIELD | CONSTR | <A HREF="#method_summary">METHOD</A></FONT></TD>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
DETAIL: FIELD | CONSTR | <A HREF="#method_detail">METHOD</A></FONT></TD>
</TR>
</TABLE>
<A NAME="skip-navbar_bottom"></A>
<!-- ======== END OF BOTTOM NAVBAR ======= -->
<HR>
<font size="-1"><a href="http://java.sun.com/cgi-bin/bugreport.cgi">Submit a bug or feature</a><br>For further API reference and developer documentation, see <a href="../../../relnotes/devdocs-vs-specs.html">Java 2 SDK SE Developer Documentation</a>. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. <p>Copyright 2003 Sun Microsystems, Inc. All rights reserved. Use is subject to <a href="../../../relnotes/license.html">license terms</a>. Also see the <a href="http://java.sun.com/docs/redist.html">documentation redistribution policy</a>.</font>
</BODY>
</HTML>