THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1
public static <T extends Object & Comparable<? super T>> T
min(Collection<? extends T> coll)

Returns the smallest valued element of the collection based on the elements'
natural order.

public static <T> Tmin(Collection<? extends T> coll,
Comparator<? super T> comp)

Returns the smallest valued element of the collection according to comp.

public static <T extends Object & Comparable<? super T>> T
max(Collection<? extends T> coll)

Returns the largest valued element of the collection based on the elements'
natural order.

public static <T> Tmax(Collection<? extends T> coll,
Comparator<? super T> comp)

Returns the largest valued element of the collection according to the
comparator comp.

The declarations of the type variables for two of these methods seem rather complex. Basically, they just
declare that T is a Comparable type. The inclusion of "extendsObject " might seem redundant, but it
changes the erasure of T to be Object rather than Comparable.


You can obtain a Comparator that will reverse the natural ordering of the objects it compares, or that of a
given comparator:


public static <T> Comparator<T>reverseOrder()

Returns a Comparator that reverses the natural ordering of the objects it
compares.

public static <T> Comparator<T>reverseOrder(Comparator<T>
comp)

Returns a Comparator the reverses the order of the given comparator.

Then there are some general convenience methods for collections:


public static <T> booleanaddAll(Collection<? super T> coll,
T... elems)

Adds all of the specified elements to the given collection, returning true if
the collection was changed. Naturally, if the collection does not support the
add method then UnsupportedOperationException is thrown.

public static booleandisjoint(Collection<?> coll1,
Collection<?> coll2)

Returns true if the two given collections have no elements in common.
Free download pdf