public final class Comparators
extends java.lang.Object
Comparator instances. For many other helpful
comparator utilities, see either Comparator itself (for Java 8 or later), or com.google.common.collect.Ordering (otherwise).
OrderingIn light of the significant enhancements to Comparator in Java 8, the overwhelming
majority of usages of Ordering can be written using only built-in JDK APIs. This class is
intended to "fill the gap" and provide those features of Ordering not already provided by
the JDK.
| Modifier | Constructor and Description |
|---|---|
private |
Comparators() |
| Modifier and Type | Method and Description |
|---|---|
static <T> java.util.Comparator<java.util.Optional<T>> |
emptiesFirst(java.util.Comparator<? super T> valueComparator)
Returns a comparator of
Optional values which treats Optional.empty() as less
than all other values, and orders the rest using valueComparator on the contained
value. |
static <T> java.util.Comparator<java.util.Optional<T>> |
emptiesLast(java.util.Comparator<? super T> valueComparator)
Returns a comparator of
Optional values which treats Optional.empty() as greater
than all other values, and orders the rest using valueComparator on the contained
value. |
static <T> java.util.stream.Collector<T,?,java.util.List<T>> |
greatest(int k,
java.util.Comparator<? super T> comparator)
Returns a
Collector that returns the k greatest (relative to the specified
Comparator) input elements, in descending order, as an unmodifiable List. |
static <T> boolean |
isInOrder(java.lang.Iterable<? extends T> iterable,
java.util.Comparator<T> comparator)
Returns
true if each element in iterable after the first is greater than or
equal to the element that preceded it, according to the specified comparator. |
static <T> boolean |
isInStrictOrder(java.lang.Iterable<? extends T> iterable,
java.util.Comparator<T> comparator)
Returns
true if each element in iterable after the first is strictly
greater than the element that preceded it, according to the specified comparator. |
static <T> java.util.stream.Collector<T,?,java.util.List<T>> |
least(int k,
java.util.Comparator<? super T> comparator)
Returns a
Collector that returns the k smallest (relative to the specified
Comparator) input elements, in ascending order, as an unmodifiable List. |
static <T,S extends T> |
lexicographical(java.util.Comparator<T> comparator)
Returns a new comparator which sorts iterables by comparing corresponding elements pairwise
until a nonzero result is found; imposes "dictionary order." If the end of one iterable is
reached, but not the other, the shorter iterable is considered to be less than the longer one.
|
static <T extends java.lang.Comparable<? super T>> |
max(T a,
T b)
Returns the maximum of the two values.
|
static <T> T |
max(T a,
T b,
java.util.Comparator<T> comparator)
Returns the maximum of the two values, according to the given comparator.
|
static <T extends java.lang.Comparable<? super T>> |
min(T a,
T b)
Returns the minimum of the two values.
|
static <T> T |
min(T a,
T b,
java.util.Comparator<T> comparator)
Returns the minimum of the two values, according to the given comparator.
|
public static <T,S extends T> java.util.Comparator<java.lang.Iterable<S>> lexicographical(java.util.Comparator<T> comparator)
[] < [1] < [1,
1] < [1, 2] < [2].
Note that Collections.reverseOrder(lexicographical(comparator)) is not equivalent to
lexicographical(Collections.reverseOrder(comparator)) (consider how each would order
[1] and [1, 1]).
public static <T> boolean isInOrder(java.lang.Iterable<? extends T> iterable,
java.util.Comparator<T> comparator)
true if each element in iterable after the first is greater than or
equal to the element that preceded it, according to the specified comparator. Note that this is
always true when the iterable has fewer than two elements.public static <T> boolean isInStrictOrder(java.lang.Iterable<? extends T> iterable,
java.util.Comparator<T> comparator)
true if each element in iterable after the first is strictly
greater than the element that preceded it, according to the specified comparator. Note that
this is always true when the iterable has fewer than two elements.public static <T> java.util.stream.Collector<T,?,java.util.List<T>> least(int k,
java.util.Comparator<? super T> comparator)
Collector that returns the k smallest (relative to the specified
Comparator) input elements, in ascending order, as an unmodifiable List. Ties
are broken arbitrarily.
For example:
Stream.of("foo", "quux", "banana", "elephant")
.collect(least(2, comparingInt(String::length)))
// returns {"foo", "quux"}
This Collector uses O(k) memory and takes expected time O(n) (worst-case O(n log
k)), as opposed to e.g. Stream.sorted(comparator).limit(k), which currently takes O(n
log n) time and O(n) space.
java.lang.IllegalArgumentException - if k < 0public static <T> java.util.stream.Collector<T,?,java.util.List<T>> greatest(int k,
java.util.Comparator<? super T> comparator)
Collector that returns the k greatest (relative to the specified
Comparator) input elements, in descending order, as an unmodifiable List. Ties
are broken arbitrarily.
For example:
Stream.of("foo", "quux", "banana", "elephant")
.collect(greatest(2, comparingInt(String::length)))
// returns {"elephant", "banana"}
This Collector uses O(k) memory and takes expected time O(n) (worst-case O(n log
k)), as opposed to e.g. Stream.sorted(comparator.reversed()).limit(k), which currently
takes O(n log n) time and O(n) space.
java.lang.IllegalArgumentException - if k < 0public static <T> java.util.Comparator<java.util.Optional<T>> emptiesFirst(java.util.Comparator<? super T> valueComparator)
Optional values which treats Optional.empty() as less
than all other values, and orders the rest using valueComparator on the contained
value.public static <T> java.util.Comparator<java.util.Optional<T>> emptiesLast(java.util.Comparator<? super T> valueComparator)
Optional values which treats Optional.empty() as greater
than all other values, and orders the rest using valueComparator on the contained
value.public static <T extends java.lang.Comparable<? super T>> T min(T a,
T b)
The recommended solution for finding the minimum of some values depends on the type
of your data and the number of elements you have. Read more in the Guava User Guide article on
Comparators.
a - first value to compare, returned if less than or equal to b.b - second value to compare.java.lang.ClassCastException - if the parameters are not mutually comparable.public static <T> T min(T a,
T b,
java.util.Comparator<T> comparator)
The recommended solution for finding the minimum of some values depends on the type
of your data and the number of elements you have. Read more in the Guava User Guide article on
Comparators.
a - first value to compare, returned if less than or equal to bb - second value to compare.java.lang.ClassCastException - if the parameters are not mutually comparable using the given
comparator.public static <T extends java.lang.Comparable<? super T>> T max(T a,
T b)
The recommended solution for finding the maximum of some values depends on the type
of your data and the number of elements you have. Read more in the Guava User Guide article on
Comparators.
a - first value to compare, returned if greater than or equal to b.b - second value to compare.java.lang.ClassCastException - if the parameters are not mutually comparable.public static <T> T max(T a,
T b,
java.util.Comparator<T> comparator)
The recommended solution for finding the maximum of some values depends on the type
of your data and the number of elements you have. Read more in the Guava User Guide article on
Comparators.
a - first value to compare, returned if greater than or equal to b.b - second value to compare.java.lang.ClassCastException - if the parameters are not mutually comparable using the given
comparator.