|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.google.common.collect.Comparators
public final class Comparators
Standard comparators and utilities for creating and working with comparators.
Comparator instances returned by this class are serializable.
| Field Summary | |
|---|---|
static Comparator<Object> |
STRING_FORM_ORDER
A comparator that compares objects by the natural ordering of their string representations as returned by toString. |
| Method Summary | ||
|---|---|---|
static int |
compare(boolean a,
boolean b)
Compares the two specified boolean values. |
|
static int |
compare(byte a,
byte b)
Compares the two specified byte values. |
|
static int |
compare(char a,
char b)
Compares the two specified char values. |
|
static int |
compare(double a,
double b)
Compares the two specified double values. |
|
static int |
compare(float a,
float b)
Compares the two specified float values. |
|
static int |
compare(int a,
int b)
Compares the two specified int values. |
|
static int |
compare(long a,
long b)
Compares the two specified long values. |
|
static int |
compare(short a,
short b)
Compares the two specified short values. |
|
static
|
compound(Comparator<? super T> primary,
Comparator<? super T> secondary,
Comparator<? super T>... rest)
Returns a comparator which tries each given comparator in order until a non-zero result is found, returning this result, and returning zero only if all comparators return zero. |
|
static
|
compound(List<? extends Comparator<? super T>> comparators)
Returns a comparator which tries each given comparator in order until a non-zero result is found, returning this result, and returning zero only if all comparators return zero. |
|
static
|
fromFunction(Function<F,T> function)
Creates a comparator that compares any two items by applying a function to each of them and using the natural ordering of the results. |
|
static
|
fromFunction(Function<F,T> function,
Comparator<? super T> comparator)
Creates a comparator that compares any two items by applying a function to each of them and using the supplied comparator to compare the results. |
|
static
|
givenOrder(List<T> valuesInOrder)
Returns a comparator that compares objects by the order in which they appear in a given list. |
|
static
|
givenOrder(T leastValue,
T... remainingValuesInOrder)
Returns a comparator that compares objects by the order in which they are given to this method. |
|
static
|
max(Comparator<? super T> comparator,
T a,
T b)
Returns the larger of the two values according to the specified comparator. |
|
static
|
max(T a,
T b)
Returns the larger of the two values, according to their natural ordering. |
|
static
|
min(Comparator<? super T> comparator,
T a,
T b)
Returns the smaller of the two values according to the specified comparator. |
|
static
|
min(T a,
T b)
Returns the smaller of the two values, according to their natural ordering. |
|
static
|
naturalOrder()
Returns a comparator that uses the natural ordering of values as defined by Comparable. |
|
static
|
nullGreatestOrder()
Returns a comparator that uses the natural ordering of values, but also handles null values, treating them as greater than all other values. |
|
static
|
nullGreatestOrder(Comparator<T> comparator)
Returns a comparator that treats null as greater than all other
values, and uses the given comparator to compare non-null values. |
|
static
|
nullLeastOrder()
Returns a comparator that uses the natural ordering of values, but also handles null values, treating them as less than all other values. |
|
static
|
nullLeastOrder(Comparator<T> comparator)
Returns a comparator that treats null as less than all other
values, and uses comparator to compare non-null values. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final Comparator<Object> STRING_FORM_ORDER
toString. Does not allow null
values.
| Method Detail |
|---|
public static <C extends Comparable> Comparator<C> naturalOrder()
Comparable.
Collections.reverseOrder()public static <T> Comparator<T> nullLeastOrder(Comparator<T> comparator)
null as less than all other
values, and uses comparator to compare non-null values.
public static <C extends Comparable> Comparator<C> nullLeastOrder()
public static <T> Comparator<T> nullGreatestOrder(Comparator<T> comparator)
null as greater than all other
values, and uses the given comparator to compare non-null values.
public static <C extends Comparable> Comparator<C> nullGreatestOrder()
public static <T> Comparator<T> compound(Comparator<? super T> primary,
Comparator<? super T> secondary,
Comparator<? super T>... rest)
The returned comparator is a "view" of the specified rest array;
changes to the array will be reflected in the behavior of the returned
comparator.
primary - the primary comparatorsecondary - the secondary comparatorrest - additional comparators to invoke as necessarycompound(List)public static <T> Comparator<T> compound(List<? extends Comparator<? super T>> comparators)
The returned comparator is a "view" of the specified List
instance; changes to the collection of comparators will be reflected in the
behavior of the returned comparator.
comparators - a collection of comparators to try in orderpublic static <F,T extends Comparable> Comparator<F> fromFunction(Function<F,T> function)
null.
function - the function returning the value to compare
public static <F,T> Comparator<F> fromFunction(Function<F,T> function,
Comparator<? super T> comparator)
function - the function returning the value to comparecomparator - the comparator that receives the function output
public static <T extends Comparable> T min(T a,
T b)
a - non-null value to compare, returned if less than or equal to b.b - non-null value to compare.
ClassCastException - if the parameters are not mutually
comparable (for example, strings and integers).
public static <T extends Comparable> T max(T a,
T b)
a - non-null value to compare, returned if greater than or equal to b.b - non-null value to compare.
ClassCastException - if the parameters are not mutually
comparable (for example, strings and integers).
public static <T> T min(Comparator<? super T> comparator,
T a,
T b)
a - non-null value to compare, returned if less than or equal to b.b - non-null value to compare.
ClassCastException - if the parameters are not mutually
comparable using the specified comparator.
public static <T> T max(Comparator<? super T> comparator,
T a,
T b)
a - non-null value to compare, returned if greater than or equal to b.b - non-null value to compare.
ClassCastException - if the parameters are not mutually
comparable using the specified comparator.public static <T> Comparator<T> givenOrder(List<T> valuesInOrder)
Object.equals(java.lang.Object)) may be compared. This comparator imposes a "partial
ordering" over the type T. Subsequent changes to the valuesInOrder list will have no effect on the returned comparator.
valuesInOrder - the values that the returned comparator will be able
to compare, in the order the comparator should follow
IllegalArgumentException - if valuesInOrder contains any
duplicate values (according to Object.equals(java.lang.Object))
public static <T> Comparator<T> givenOrder(@Nullable
T leastValue,
T... remainingValuesInOrder)
Object.equals(java.lang.Object)) may be compared. This comparator imposes a
"partial ordering" over the type T.
leastValue - the value which the returned comparator should consider
the "least" of all valuesremainingValuesInOrder - the rest of the values that the returned
comparator will be able to compare, in the order the comparator should
follow
IllegalArgumentException - if any duplicate values (according to
Object.equals(java.lang.Object)) are present among leastValue and remainingValuesInOrder
public static int compare(byte a,
byte b)
byte values. The sign of the value
returned is the same as that of the value that would be returned by the
call:
new Byte(a).compareTo(new Byte(b))
a - the first byte to compareb - the second byte to compare
a is less than b; a positive
value if a is greater than b; otherwise zero.
public static int compare(char a,
char b)
char values. The sign of the value
returned is the same as that of the value that would be returned by the
call:
new Character(a).compareTo(new Character(b))
a - the first char to compareb - the second char to compare
a is less than b; a positive
value if a is greater than b; otherwise zero.
public static int compare(short a,
short b)
short values. The sign of the value
returned is the same as that of the value that would be returned by the
call:
new Short(a).compareTo(new Short(b))
a - the first short to compareb - the second short to compare
a is less than b; a positive
value if a is greater than b; otherwise zero.
public static int compare(int a,
int b)
int values. The sign of the value
returned is the same as that of the value that would be returned by the
call:
new Integer(a).compareTo(new Integer(b))
a - the first int to compareb - the second int to compare
a is less than b; a positive
value if a is greater than b; otherwise zero.
public static int compare(long a,
long b)
long values. The sign of the value
returned is the same as that of the value that would be returned by the
call:
new Long(a).compareTo(new Long(b))
a - the first long to compareb - the second long to compare
a is less than b; a positive
value if a is greater than b; otherwise zero.
public static int compare(double a,
double b)
double values. The sign of the value
returned is the same as that of the value that would be returned by the
call:
new Double(a).compareTo(new Double(b))
a - the first double to compareb - the second double to compare
a is less than b; a positive
value if a is greater than b; otherwise zero.Double.compare(double, double)
public static int compare(float a,
float b)
float values. The sign of the value
returned is the same as that of the value that would be returned by the
call:
new Float(a).compareTo(new Float(b))
a - the first float to compareb - the second float to compare
a is less than b; a positive
value if a is greater than b; otherwise zero.Float.compare(float, float)
public static int compare(boolean a,
boolean b)
boolean values. The sign of the value
returned is the same as that of the value that would be returned by the
call:
new Boolean(a).compareTo(new Boolean(b))
a - the first boolean to compareb - the second boolean to compare
a is false and b is true; a
positive value if a is true and b is false; otherwise
zero.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||