/** * Returns a comparator that compares {@link Comparable} objects in natural * order. * * <p>The returned comparator is serializable and throws {@link * NullPointerException} when comparing {@code null}. * * @param <T> the {@link Comparable} type of element to be compared * @return a comparator that imposes the <i>natural ordering</i> on {@code * Comparable} objects. * @see Comparable * @since 1.8 */ @SuppressWarnings("unchecked") public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() { return (Comparator<T>) Comparators.NaturalOrderComparator.INSTANCE; }
/** * Returns a null-friendly comparator that considers {@code null} to be * less than non-null. When both are {@code null}, they are considered * equal. If both are non-null, the specified {@code Comparator} is used * to determine the order. If the specified comparator is {@code null}, * then the returned comparator considers all non-null values to be equal. * * <p>The returned comparator is serializable if the specified comparator * is serializable. * * @param <T> the type of the elements to be compared * @param comparator a {@code Comparator} for comparing non-null values * @return a comparator that considers {@code null} to be less than * non-null, and compares non-null objects with the supplied * {@code Comparator}. * @since 1.8 */ public static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator) { return new Comparators.NullComparator<>(true, comparator); }
/** * Returns a null-friendly comparator that considers {@code null} to be * greater than non-null. When both are {@code null}, they are considered * equal. If both are non-null, the specified {@code Comparator} is used * to determine the order. If the specified comparator is {@code null}, * then the returned comparator considers all non-null values to be equal. * * <p>The returned comparator is serializable if the specified comparator * is serializable. * * @param <T> the type of the elements to be compared * @param comparator a {@code Comparator} for comparing non-null values * @return a comparator that considers {@code null} to be greater than * non-null, and compares non-null objects with the supplied * {@code Comparator}. * @since 1.8 */ public static <T> Comparator<T> nullsLast(Comparator<? super T> comparator) { return new Comparators.NullComparator<>(false, comparator); }