The only method in the Java. Lang. Comparable interface is compareto (). In this method, you can perform a simple equal comparison and execution sequence comparison. The interface implementation framework is as follows:
Public class comparableimpl implements comparable <comparableimpl >{</P> <p> @ override <br/> Public int compareto (comparableimpl O) {<br/> // todo auto-generated method stub <br/> return 0; <br/>}</P> <p>}
If a class implements the comparable interface, it indicates that its instance has an internal sorting relationship, and it can be connected to a variety of genericAlgorithmAnd depends on the set implementation of this interface for collaboration. Classes dependent on the comparison relationship include the ordered collection class treeset and treemap, and the tool class collections and arrays.
If the elements in an array implement the comparable interface, you can directly sort the Array Using the sort method of the arrays class. All value classes in the Java platform library implement the comparable interface.
Comparable specifications: compares the current object with the specified object in sequence. If the object is smaller than, equal to, or greater than the specified object, a negative integer, zero, or positive integer is returned. If the comparison cannot be performed because of the specified object type, a classcastexception exception is thrown.
The compareto method must meet the following constraints: inversion, symmetry, transmission, and non-null.
In general, the equal test of the Comparato method should return the same result as that of the equals method. If they are the same, the ordered relationship applied by the compareto method is called "Same as equals". If they are different, the ordered relationship is called "inconsistent with equals ". If the order relationship between the compareto method and the equals method of a class is inconsistent, it still works normally. However, if an ordered set contains instances of the class, the set may not follow the general conventions of some set interfaces. Because the general conventions of the Set interface are defined according to the equals method, the ordered set uses the equality test applied by compareto. The following is a class that implements the comparable interface. At the same time, the class also overwrites equals, hashcode, and other methods:
Public abstract class zltextposition implements comparable <zltextposition >{</P> <p> public abstract int getparagraphindex (); <br/> public abstract int getelementindex (); <br/> public abstract int getcharindex (); </P> <p> Public Boolean samepositionas (zltextposition position) {<br/> return <br/> getparagraphindex () = position. getparagraphindex () & <br/> getelementindex () = position. getelementindex () & <B R/> getcharindex () = position. getcharindex (); <br/>}</P> <p> @ override <br/> Public int compareto (zltextposition position) {<br/> final int p0 = getparagraphindex (); <br/> final int p1 = position. getparagraphindex (); <br/> If (P0! = P1) {<br/> return P0 <P1? -1: 1; <br/>}</P> <p> final int e0 = getelementindex (); <br/> final int e1 = position. getelementindex (); <br/> If (E0! = E1) {<br/> return E0 <E1? -1: 1; <br/>}</P> <p> final int C0 = getcharindex (); <br/> final int C1 = position. getcharindex (); <br/> If (C0! = C1) {<br/> return C0 <C1? -1: 1; <br/>}< br/> return 0; <br/>}</P> <p> @ override <br/> Public Boolean equals (Object OBJ) {<br/> If (this = OBJ) {<br/> return true; <br/>}< br/> If (! (OBJ instanceof zltextposition) {<br/> return false; <br/>}</P> <p> final zltextposition position = (zltextposition) OBJ; <br/> return samepositionas (position); <br/>}</P> <p> @ override <br/> Public int hashcode () {<br/> return (getparagraphindex () <16) + (getelementindex () <8) + getcharindex (); <br/>}</P> <p> @ override <br/> Public String tostring () {<br/> return getclass (). getname () + "" + getparagraphindex () + "" + getelementindex () + "" + getcharindex (); <br/>}</P> <p>}