Implementing the java.util.Comparator Interface 3 – Object Comparison
Output from the program:
List before sorting:
[(3.49.1), (8.19.81), (2.48.28), (10.23.78), (9.1.1)]
List after sorting according to reverse natural ordering:
[(10.23.78), (9.1.1), (8.19.81), (3.49.1), (2.48.28)]
Binary search in list using reverse natural ordering found key (9.1.1) at index: 1
Binary search in list using natural ordering found key (9.1.1) at index: -6
The Comparator<E> interface also provides many useful static and default methods, including composing conditional comparators that can compare on multiple fields. Reference is given in the description below to where the methods are used.
default Comparator<T> reversed()
static <T extends Comparable<? super T>> Comparator<T> naturalOrder()
static <T extends Comparable<? super T>> Comparator<T> reverseOrder()
The first method returns a comparator that imposes the reverse ordering of this comparator, equivalent to (a, b) -> this.compare(b, a).
The second method returns a comparator that compares Comparable objects in natural order, equivalent to (a, b) -> a.compareTo(b).
The third method returns a comparator that imposes the reverse of the natural ordering on Comparable objects, equivalent to (a, b) -> b.compareTo(a).
See Example 14.10, p. 770, and Example 14.11, p. 772.
static <T> Comparator<T> nullsFirst(Comparator<? super T> cmp)
static <T> Comparator<T> nullsLast(Comparator<? super T> cmp)
Return a null-friendly comparator that considers null to be either less than non-null or greater than non-null, respectively. These are useful comparators for sorting or searching in collections and maps when nulls are considered as actual values.
static <T, U> Comparator<T>
comparing(Function<? super T,? extends U> func,
Comparator<? super U> cmp)
Returns a Comparator<T> that applies func to the two given elements and compares the results using the specified Comparator cmp. It is effectively equivalent to (a, b) -> cmp.compare(func.apply(a), func.apply(b)).
static <T, U extends Comparable<? super U>> Comparator<T>
comparing(Function<? super T,? extends U> func)
Returns a Comparator<T> that applies func to the two given elements first, before comparing the results by natural ordering. It is effectively equivalent to (a, b) -> func.apply(a).compareTo(func.apply(b)).
static <T> Comparator<T>
comparing
PrimType
(To
PrimType
Function<? super T> func)
Returns a Comparator<T> that applies func to the two given elements first, before comparing the primitive-value results.
PrimType is either an Int, Long, or Double.
See Example 14.8, p. 763, and Example 14.10, p. 770.
default Comparator<T>
thenComparing(Comparator<? super T> cmp)
Returns a conditional comparator that first determines using this Comparator whether two given elements are equal. If they are equal, the elements are compared using the specified Comparator cmp. Effectively, this method first executes this.compare(a, b), and then cmp.compare(a, b) if necessary.
See Example 14.10, p. 770.
default <U> Comparator<T>
thenComparing(Function<? super T,? extends U> func,
Comparator<? super U> cmp)
Returns a conditional comparator that first determines using this Comparator whether two given elements are equal. If they are equal, it then applies func to the elements and the results are compared using the specified Comparator cmp. Effectively, this method first executes this.compare(a, b), and then cmp.compare (func.apply(a), func.apply(b)) if necessary.
default <U extends Comparable<? super U>> Comparator<T>
thenComparing(Function<? super T,? extends U> func)
Returns a conditional comparator that first determines using this Comparator whether two given elements are equal. If they are equal, it then applies func to the elements and the results are compared by natural ordering. Effectively, this method first executes this.compare(a, b), and then func.apply(a).compareTo (func.apply(b)) if necessary.
default Comparator<T>
thenComparing
PrimType
(To
PrimType
Function<? super T> func)
PrimType is either an Int, Long, or Double.
These primitive-type specialized methods return a conditional comparator that first determines, using this Comparator, if two given elements are equal. If they are equal, it then applies func to the two elements and the primitive-value results are compared.
See Example 14.8, p. 763, and Example 14.10, p. 770.