Java Reference
In-Depth Information
Table 13.2
Useful Comparators and Methods
Comparator/Method
Description
Arrays.binarySearch(array, Returns the index of the given value in the given
value, comparator) array, assuming that the array is currently sorted in
the ordering of the given comparator, or returns a
negative number if the given value is not found.
Arrays.sort(array, comparator) Sorts the given array in the ordering of the given
comparator.
Collections.binarySearch(list, Returns the index of the given value in the given
value, comparator)
list, assuming that the list is currently sorted in the
ordering of the given comparator, or returns a nega-
tive number if the given value is not found.
Returns the largest value in the collection
Collections.max(collection,
according to the ordering of the given comparator.
comparator)
Returns the smallest value in the collection
Collections.min(collection,
according to the ordering of the given comparator.
comparator)
Returns a comparator that compares objects in the
opposite of their natural order.
Collections.reverseOrder()
Returns a comparator that compares objects in the
Collections.reverseOrder
opposite of the ordering of the given comparator.
(comparator)
Sorts the given list in the ordering of the given
Collections.sort(list,
comparator.
comparator)
Sorts strings alphabetically, ignoring capitalization.
String.CASE_INSENSITIVE_ORDER
Comparator to be reversed. For example, the following code would sort the array of
Point objects by decreasing x -coordinate:
Arrays.sort(points, Collections.reverseOrder(new PointComparator()));
The resulting order would be as follows: (4, -2), (3, 9), (3, 7), (-1, 15).
Table 13.2 summarizes several useful places that comparators appear in the Java
class libraries.
13.2 Program Complexity
In Chapter 8 we talked about client code, the code that interacts with a class or
object. In this chapter we've studied how to be a client of Java's methods for search-
ing and sorting data. Since searching and sorting are important programming ideas,
it's worthwhile to understand how they are implemented. But before we dive into
this, let's discuss some background ideas about how to analyze the efficiency of code.
 
 
Search WWH ::




Custom Search