@FunctionalInterface
public interface Ordering<T>
order.compare(a, b) == order.compare(b, a).inverse()
for any two values of a and b.
Note that this also implies order.compare(a,a) == Order.Same
as Order.Same is the only result which is its own inverse.
Ordering is very much the same as a Comparator, it just
has a nicer result. Of course method asComparator()
converts an ordering into a Comparator, and static factory
method fromComparator(Comparator) does the inverse.
It is recommended that Comparators are serializable for
usage with TreeMap and similar, and so this is true
for orderings. See Ordering.Serial to get a general example.
| Modifier and Type | Interface and Description |
|---|---|
static interface |
Ordering.Serial<E>
An ordering which is serializable.
|
| Modifier and Type | Method and Description |
|---|---|
default <S extends java.lang.Comparable<? super S>> |
andIfSame(java.util.function.Function<? super T,? extends S> keyExtractor) |
default <S> Ordering<T> |
andIfSame(java.util.function.Function<? super T,? extends S> keyExtractor,
Ordering<? super S> keyOrdering) |
default Ordering<T> |
andIfSame(Ordering<? super T> lowerOrder)
Combine this ordering with a lower-level ordering.
|
default boolean |
ascending(T v1,
T v2)
Are the two values represented in strictly ascending order?
|
default boolean |
ascendingOrSame(T v1,
T v2)
Are the two values represented in ascending order?
|
default java.util.Comparator<T> |
asComparator()
Use this ordering as a comparator.
|
default java.util.function.BiPredicate<T,T> |
asEqualityChecker()
Use this ordering as a checker for equality.
|
Order |
check(T v1,
T v2)
Check the ordering of the two values.
|
default boolean |
descending(T v1,
T v2)
Are the two values represented in strictly descending order?
|
default boolean |
descendingOrSame(T v1,
T v2)
Are the two values represented in descending order?
|
default boolean |
different(T v1,
T v2)
Are the two values considered different by this order?
|
static <E> Ordering<E> |
fromComparator(java.util.Comparator<E> comparator)
Convert a standard comparator of
Double into a double ordering. |
default Ordering<T> |
inverse()
Invert this order.
|
static <E extends java.lang.Comparable<? super E>> |
inverseNatural()
Get the inverse of the natural order of a comparable type.
|
static <E extends java.lang.Comparable<? super E>> |
natural()
Get the natural order of a comparable type.
|
default Ordering<T> |
nullsFirst()
Make this ordering null-safe in handling
null values
as coming before all other values. |
default Ordering<T> |
nullsLast()
Make this ordering null-safe in handling
null values
as coming after all other values. |
static <S,K extends java.lang.Comparable<? super K>> |
ordering(java.util.function.Function<? super S,? extends K> keyExtractor) |
static <S,K> Ordering<S> |
ordering(java.util.function.Function<? super S,? extends K> keyExtractor,
Ordering<? super K> keyOrdering) |
static <S> Ordering<S> |
orderingDouble(java.util.function.ToDoubleFunction<? super S> valueExtractor) |
static <S> Ordering<S> |
orderingDouble(java.util.function.ToDoubleFunction<? super S> valueExtractor,
DoubleOrdering ordering) |
default boolean |
same(T v1,
T v2)
Are the two values considered equal by this order?
|
@NotNull Order check(T v1, T v2)
v1 - first valuev2 - second valuedefault boolean ascending(T v1, T v2)
v1 - first valuev2 - second valuetrue if v1 < v2 according to this orderingfalse if v1 >= v2 according to this orderingdescending(Object, Object),
ascendingOrSame(Object, Object),
descendingOrSame(Object, Object),
same(Object, Object),
different(Object, Object)default boolean ascendingOrSame(T v1, T v2)
v1 - first valuev2 - second valuetrue if v1 <= v2 according to this orderingfalse if v1 > v2 according to this orderingascending(Object, Object),
descending(Object, Object),
descendingOrSame(Object, Object),
same(Object, Object),
different(Object, Object)default boolean descending(T v1, T v2)
v1 - first valuev2 - second valuetrue if v1 > v2 according to this orderingfalse if v1 <= v2 according to this orderingascending(Object, Object),
ascendingOrSame(Object, Object),
descendingOrSame(Object, Object),
same(Object, Object),
different(Object, Object)default boolean descendingOrSame(T v1, T v2)
v1 - first valuev2 - second valuetrue if v1 >= v2 according to this orderingfalse if v1 < v2 according to this orderingascending(Object, Object),
descending(Object, Object),
ascendingOrSame(Object, Object),
same(Object, Object),
different(Object, Object)default boolean same(T v1, T v2)
v1 - first valuev2 - second valuetrue if v1 == v2 according to this orderfalse if v1 != v2 according to this orderingascending(Object, Object),
descending(Object, Object),
ascendingOrSame(Object, Object),
descendingOrSame(Object, Object),
different(Object, Object)default boolean different(T v1, T v2)
v1 - first valuev2 - second valuetrue if v1 != v2 according to this orderfalse if v1 == v2 according to this orderingascending(Object, Object),
descending(Object, Object),
ascendingOrSame(Object, Object),
descendingOrSame(Object, Object),
same(Object, Object)default Ordering<T> nullsFirst()
null values
as coming before all other values.null values firstdefault Ordering<T> nullsLast()
null values
as coming after all other values.null values last@NotNull default java.util.Comparator<T> asComparator()
Double@NotNull default Ordering<T> andIfSame(@NotNull Ordering<? super T> lowerOrder)
equal.lowerOrder - lower order@NotNull default <S> Ordering<T> andIfSame(@NotNull java.util.function.Function<? super T,? extends S> keyExtractor, @NotNull Ordering<? super S> keyOrdering)
@NotNull default <S extends java.lang.Comparable<? super S>> Ordering<T> andIfSame(@NotNull java.util.function.Function<? super T,? extends S> keyExtractor)
@NotNull static <S,K> Ordering<S> ordering(@NotNull java.util.function.Function<? super S,? extends K> keyExtractor, @NotNull Ordering<? super K> keyOrdering)
@NotNull static <S,K extends java.lang.Comparable<? super K>> Ordering<S> ordering(@NotNull java.util.function.Function<? super S,? extends K> keyExtractor)
@NotNull static <S> Ordering<S> orderingDouble(@NotNull java.util.function.ToDoubleFunction<? super S> valueExtractor, @NotNull DoubleOrdering ordering)
@NotNull static <S> Ordering<S> orderingDouble(@NotNull java.util.function.ToDoubleFunction<? super S> valueExtractor)
@NotNull default java.util.function.BiPredicate<T,T> asEqualityChecker()
@NotNull static <E> Ordering<E> fromComparator(@NotNull java.util.Comparator<E> comparator)
Double into a double ordering.E - type on which the order is definedcomparator - comparator of Double@NotNull static <E extends java.lang.Comparable<? super E>> Ordering<E> natural()
E - comparable type