ComparableT


Comparable<T> java.lang

Java 1.2 comparable

This interface defines a single method, compareTo( ) , that is responsible for comparing one object to another and determining their relative order, according to some natural ordering for that class of objects. Any general-purpose class that represents a value that can be sorted or ordered should implement this interface. Any class that does implement this interface can make use of various powerful methods such as java.util.Collections. sort ( ) and java.util.Arrays.binarySearch( ) . Many of the key classes in the Java API implement this interface. In Java 5.0, this interface has been made generic. The type variable T represents the type of the object that is passed to the compareTo( ) method.

The compareTo( ) method compares this object to the object passed as an argument. It should assume that the supplied object is of the appropriate type; if it is not, it should throw a ClassCastException . If this object is less than the supplied object or should appear before the supplied object in a sorted list, compareTo( ) should return a negative number. If this object is greater than the supplied object or should come after the supplied object in a sorted list, compareTo( ) should return a positive integer. If the two objects are equivalent, and their relative order in a sorted list does not matter, compareTo( ) should return 0. If compareTo( ) returns 0 for two objects, the equals( ) method should typically return true . If this is not the case, the Comparable objects are not suitable for use in java.util.TreeSet and java.util.TreeMap classes.

See java.util.Comparator for a way to define an ordering for objects that do not implement Comparable or to define an ordering other than the natural ordering defined by a Comparable class.

 public interface  Comparable<T>  {  // Public Instance Methods  int  compareTo  (T  o  );   } 

Implementations

Too many classes to list.



Java In A Nutshell
Java In A Nutshell, 5th Edition
ISBN: 0596007736
EAN: 2147483647
Year: 2004
Pages: 1220

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net