Interface Comparator<T>


public interface Comparator<T>

A Comparator is used to compare two objects to determine their ordering with respect to each other. On a given Collection, a Comparator can be used to obtain a sorted Collection which is totally ordered. For a Comparator to be consistent with equals, its {code #compare(Object, Object)} method has to return zero for each pair of elements (a,b) where a.equals(b) holds true. It is recommended that a Comparator implements java.io.Serializable.

Since

1.2

  • Method Details

    • compare

      int compare(T object1, T object2)

      Compares the two specified objects to determine their relative ordering. The ordering implied by the return value of this method for all possible pairs of (object1, object2) should form an equivalence relation. This means that

      • compare(a,a) returns zero for all a

      • the sign of compare(a,b) must be the opposite of the sign of compare(b,a) for all pairs of (a,b)

      • From compare(a,b) > 0 and compare(b,c) > 0 it must follow compare(a,c) > 0 for all possible combinations of (a,b,c)

      Parameters
      • object1: an Object.

      • object2: a second Object to compare with object1.

      Returns

      an integer 0 if object1 is greater than object2.

      Throws
      • ClassCastException: if objects are not of the correct type.
    • equals

      boolean equals(Object object)

      Compares this Comparator with the specified Object and indicates whether they are equal. In order to be equal, object must represent the same object as this instance using a class-specific comparison.

      A Comparator never needs to override this method, but may choose so for performance reasons.

      Parameters
      • object: the Object to compare with this comparator.
      Returns
      Overrides:
      equals in class Object
      Returns:

      boolean true if specified Object is the same as this Object, and false otherwise.

      See also
      • Object#hashCode

      • Object#equals

    • reversed

      default Comparator<T> reversed()
      This ordering, reversed.
      Returns:
      the reversed comparator
    • thenComparing

      default Comparator<T> thenComparing(Comparator<? super T> other)
      This ordering, ties broken by other.
      Parameters:
      other - the tie breaker
      Returns:
      the combined comparator
    • thenComparing

      default <U extends Comparable<? super U>> Comparator<T> thenComparing(Function<? super T, ? extends U> keyExtractor)
      This ordering, ties broken by the natural order of a key.
      Type Parameters:
      U - the key type
      Parameters:
      keyExtractor - extracts the key
      Returns:
      the combined comparator
    • comparing

      static <T, U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor)
      Compares by the natural order of a key.
      Type Parameters:
      T - the compared type
      U - the key type
      Parameters:
      keyExtractor - extracts the key
      Returns:
      the comparator
    • comparing

      static <T,U> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator)
      Compares by a key, using the given comparator for the keys.
      Type Parameters:
      T - the compared type
      U - the key type
      Parameters:
      keyExtractor - extracts the key
      keyComparator - orders the keys
      Returns:
      the comparator
    • naturalOrder

      static <T extends Comparable<? super T>> Comparator<T> naturalOrder()
      The natural order of comparable values.
      Type Parameters:
      T - the compared type
      Returns:
      the comparator
    • reverseOrder

      static <T extends Comparable<? super T>> Comparator<T> reverseOrder()
      The reverse of the natural order.
      Type Parameters:
      T - the compared type
      Returns:
      the comparator
    • thenComparing

      default <U> Comparator<T> thenComparing(Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator)
      This ordering, ties broken by a key in the order keyComparator gives.
      Type Parameters:
      U - the key type
      Parameters:
      keyExtractor - extracts the key
      keyComparator - orders the keys
      Returns:
      the combined comparator
    • thenComparingInt

      default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor)
      This ordering, ties broken by an int key.
      Parameters:
      keyExtractor - extracts the key
      Returns:
      the combined comparator
    • thenComparingLong

      default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor)
      This ordering, ties broken by a long key.
      Parameters:
      keyExtractor - extracts the key
      Returns:
      the combined comparator
    • thenComparingDouble

      default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor)
      This ordering, ties broken by a double key, compared as Double.compare(double, double) does.
      Parameters:
      keyExtractor - extracts the key
      Returns:
      the combined comparator
    • comparingInt

      static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor)
      Compares by an int key.
      Type Parameters:
      T - the compared type
      Parameters:
      keyExtractor - extracts the key
      Returns:
      the comparator
    • comparingLong

      static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor)
      Compares by a long key.
      Type Parameters:
      T - the compared type
      Parameters:
      keyExtractor - extracts the key
      Returns:
      the comparator
    • comparingDouble

      static <T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor)
      Compares by a double key, as Double.compare(double, double) does: NaN above everything and -0.0 below 0.0.
      Type Parameters:
      T - the compared type
      Parameters:
      keyExtractor - extracts the key
      Returns:
      the comparator
    • nullsFirst

      static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator)
      Orders null before every other value, and the rest by comparator; with a null comparator the others are all equal.
      Type Parameters:
      T - the compared type
      Parameters:
      comparator - orders the values that are not null, or null
      Returns:
      the comparator
    • nullsLast

      static <T> Comparator<T> nullsLast(Comparator<? super T> comparator)
      Orders null after every other value, and the rest by comparator; with a null comparator the others are all equal.
      Type Parameters:
      T - the compared type
      Parameters:
      comparator - orders the values that are not null, or null
      Returns:
      the comparator