ComparatorT


Comparator<T> java.util

Java 1.2

This interface defines a compare( ) method that specifies a total ordering for a set of objects, allowing those objects to be sorted. The Comparator is used when the objects to be ordered do not have a natural ordering defined by the Comparable interface, or when you want to order them using something other than their natural ordering. Comparator has been made generic in Java 5.0 and the type variable T represents the type of objects being compared.

The compare( ) method is passed two objects. If the first argument is less than the second argument or should be placed before the second argument in a sorted list, compare( ) should return a negative integer. If the first argument is greater than the second argument or should be placed after the second argument in a sorted list, compare( ) should return a positive integer. If the two objects are equivalent or if their relative position in a sorted list does not matter, compare( ) should return . Comparator implementations may assume that both Object arguments are of appropriate types and cast them as desired. If either argument is not of the expected type, the compare( ) method throws a ClassCastException .

Note that the magnitude of the numbers returned by compare( ) does not matter, only whether they are less than, equal to, or greater than zero. In most cases, you should implement a Comparator so that compare(o1,o2) returns if and only if o1.equals(o2) returns TRue . This is particularly important when using a Comparator to impose an ordering on a treeSet or a treeMap .

See Collections and Arrays for various methods that use Comparator objects for sorting and searching. See also the related java.lang.Comparable interface.

 public interface  Comparator<T>  {  // Public Instance Methods  int  compare  (T  o1  , T  o2  );        boolean  equals  (Object  obj  );   } 

Implementations

java.text.Collator

Passed To

Arrays.{binarySearch( ) , sort ( )} , Collections.{binarySearch( ) , max( ) , min( ) , reverseOrder( ) , sort( )} , PriorityQueue.PriorityQueue( ) , treeMap.TreeMap( ) , treeSet.TreeSet( ) , java.util.concurrent.PriorityBlockingQueue.PriorityBlockingQueue( )

Returned By

Collections.reverseOrder( ) , PriorityQueue.comparator( ) , SortedMap.comparator( ) , SortedSet.comparator( ) , treeMap.comparator( ) , TReeSet.comparator( ) , java.util.concurrent.PriorityBlockingQueue.comparator( )

Type Of

String.CASE_INSENSITIVE_ORDER



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