📄 comparable.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd"><!--NewPage--><HTML><HEAD><!-- Generated by javadoc on Thu Apr 27 23:34:57 PDT 2000 --><TITLE>Java 2 Platform SE v1.3: Interface Comparable</TITLE><LINK REL ="stylesheet" TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style"></HEAD><BODY BGCOLOR="white"><!-- ========== START OF NAVBAR ========== --><A NAME="navbar_top"><!-- --></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0"><TR><TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1"><A NAME="navbar_top_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3"> <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.3</b></EM></TD></TR><TR><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="../../java/lang/Cloneable.html"><B>PREV CLASS</B></A> <A HREF="../../java/lang/Runnable.html"><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></FONT></TD></TR><TR><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2"> SUMMARY: INNER | 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><!-- =========== END OF 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/lang/Character.html">Character</A>, <A HREF="../../java/io/File.html">File</A>, <A HREF="../../java/lang/Long.html">Long</A>, <A HREF="../../java/io/ObjectStreamField.html">ObjectStreamField</A>, <A HREF="../../java/lang/Short.html">Short</A>, <A HREF="../../java/lang/String.html">String</A>, <A HREF="../../java/lang/Float.html">Float</A>, <A HREF="../../java/lang/Integer.html">Integer</A>, <A HREF="../../java/lang/Byte.html">Byte</A>, <A HREF="../../java/lang/Double.html">Double</A>, <A HREF="../../java/math/BigInteger.html">BigInteger</A>, <A HREF="../../java/math/BigDecimal.html">BigDecimal</A>, <A HREF="../../java/util/Date.html">Date</A>, <A HREF="../../java/text/CollationKey.html">CollationKey</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>.<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.<p> A class's natural ordering 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>.<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> operation.<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.<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>BigDecimals</tt> with equal values and different precisions (such as 4.0 and 4.00).<p> For the mathematically inclined, the <i>relation</i> that defines the natural ordering on a given class C is:<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:<pre> {(x, y) such that x.equals((Object)y)}. </pre><P><DL><DT><B>Since: </B><DD>1.2</DD><DT><B>See Also: </B><DD><A HREF="../../java/util/Comparator.html"><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"><CODE>SortedSet</CODE></A>, <A HREF="../../java/util/SortedMap.html"><CODE>SortedMap</CODE></A>, <A HREF="../../java/util/TreeSet.html"><CODE>TreeSet</CODE></A>, <A HREF="../../java/util/TreeMap.html"><CODE>TreeMap</CODE></A></DL><HR><P><!-- ======== INNER CLASS SUMMARY ======== --><!-- =========== FIELD SUMMARY =========== --><!-- ======== CONSTRUCTOR SUMMARY ======== --><!-- ========== METHOD SUMMARY =========== --><A NAME="method_summary"><!-- --></A><TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%"><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">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" CELLPADDING="3" CELLSPACING="0" WIDTH="100%"><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">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.<p> 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.)<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>.<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>.<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."<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.<DT><B>Throws:</B><DD><CODE><A HREF="../../java/lang/ClassCastException.html">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 NAVBAR ========== --><A NAME="navbar_bottom"><!-- --></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0"><TR><TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1"><A NAME="navbar_bottom_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3"> <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.3</b></EM></TD></TR><TR><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="../../java/lang/Cloneable.html"><B>PREV CLASS</B></A> <A HREF="../../java/lang/Runnable.html"><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></FONT></TD></TR><TR><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2"> SUMMARY: INNER | 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><!-- =========== END OF 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="http://java.sun.com/products/jdk/1.3/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>Java, Java 2D, and JDBC are trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries.<br>Copyright 1993-2000 Sun Microsystems, Inc. 901 San Antonio Road<br>Palo Alto, California, 94303, U.S.A. All Rights Reserved.</font></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -