1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| @FunctionalInterface public interface Comparator<T>{
int compare(T o1, T o2);
boolean equals(Object obj);
default Comparator<T> reversed(){ return Collections.reverseOrder(this); }
default Comparator<T> thenComparing(Comparator<? super T> other){ Objects.requireNonNull(other); return (Comparator<T> & Serializable)(c1,c2)->{ int res = compare(c1,c2); return (res != 0) > ? res :other.compare(c1,c2); } }
default <U> Comparator<T> thenComparing(Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator){ return thenComparing(comparing(keyExtractor,keyComparator)); }
default <U extends Comparable<? super U>> Comparator<T> thenComparing( Function<? super T, ? extends U> keyExtractor){ return thenComparing(comparing(keyExtractor)); }
default Comparator<T> thenComparing(ToIntFunction<? super T> keyExtractor){ return thenComparing(comparingInt(keyExtractor)); }
default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor) { return thenComparing(comparingLong(keyExtractor)); }
default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor) { return thenComparing(comparingDouble(keyExtractor)); }
public static <T extends Comparable<? super T>> Comparator<T> reverseOrder() { return Collections.reverseOrder(); }
public static <T extends Comparable<? super T>> Comparator<T> naturalOrder(){ return (Comparator<T>) Comparators.NaturalOrderComparator.INSTANCE; }
public static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator){ return new Comparators.NullComparator<>(true,comparator); }
public static <T> Comparator<T> nullsLast(Comparator<? super T> comparator){ return new Comparators.NullComparator<>(false,comparator); }
public static <T,U> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor,Comparator<? super U> keyComparator){ Objects.requireNonNull(keyExtractor); Objects.requireNonNull(keyComparator); return (Comparator<T> & Serializable)(c1,c2) ->keyComparator.compare(keyExtractor.apply(c1),keyExtractor.apply(c2)); }
public static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator<T> & Serializable) (c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2)); }
public static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator<T> & Serializable) (c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2)); }
public static<T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator<T> & Serializable) (c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2)); }
}
|