Interface SerializableComparator<T>

    • Method Detail

      • reversed

        default SerializableComparator<T> reversed()
        Returns a comparator that imposes the reverse ordering of this comparator.
        Specified by:
        reversed in interface Comparator<T>
        Returns:
        a comparator that imposes the reverse ordering of this comparator.
        Since:
        1.8
      • thenComparing

        default SerializableComparator<T> thenComparing​(SerializableComparator<? super T> other)
        Returns a lexicographic-order comparator with another comparator. If this SerializableComparator considers two elements equal, i.e. compare(a, b) == 0, other is used to determine the order.
        Parameters:
        other - the other comparator to be used when this comparator compares two objects that are equal.
        Returns:
        a lexicographic-order comparator composed of this and then the other comparator
        Throws:
        NullPointerException - if the argument is null.
        Since:
        1.8
        API Note:
        For example, to sort a collection of String based on the length and then case-insensitive natural ordering, the comparator can be composed using following code,
        
             SerializableComparator<String> cmp = SerializableComparator.comparingInt(String::length)
                     .thenComparing(String.CASE_INSENSITIVE_ORDER);
         
      • thenComparing

        default <U> SerializableComparator<T> thenComparing​(SerializableFunction<? super T,​? extends U> keyExtractor,
                                                            SerializableComparator<? super U> keyComparator)
        Returns a lexicographic-order comparator with a function that extracts a key to be compared with the given SerializableComparator.
        Type Parameters:
        U - the type of the sort key
        Parameters:
        keyExtractor - the function used to extract the sort key
        keyComparator - the SerializableComparator used to compare the sort key
        Returns:
        a lexicographic-order comparator composed of this comparator and then comparing on the key extracted by the keyExtractor function
        Throws:
        NullPointerException - if either argument is null.
        Since:
        1.8
        See Also:
        comparing(SerializableFunction, SerializableComparator), thenComparing(SerializableComparator)
        Implementation Requirements:
        This default implementation behaves as if thenComparing(comparing(keyExtractor, cmp)).
      • reverseOrder

        static <T extends Comparable<? super T>> SerializableComparator<T> reverseOrder()
        Returns a comparator that imposes the reverse of the natural ordering.

        The returned comparator throws NullPointerException when comparing null.

        Type Parameters:
        T - the Comparable type of element to be compared
        Returns:
        a comparator that imposes the reverse of the natural ordering on Comparable objects.
        Since:
        1.8
        See Also:
        Comparable
      • naturalOrder

        static <T extends Comparable<? super T>> SerializableComparator<T> naturalOrder()
        Returns a comparator that compares Comparable objects in natural order.

        The returned comparator throws NullPointerException when comparing null.

        Type Parameters:
        T - the Comparable type of element to be compared
        Returns:
        a comparator that imposes the natural ordering on Comparable objects.
        Since:
        1.8
        See Also:
        Comparable
      • nullsFirst

        static <T> SerializableComparator<T> nullsFirst​(SerializableComparator<? super T> comparator)
        Returns a null-friendly comparator that considers null to be less than non-null. When both are null, they are considered equal. If both are non-null, the specified Comparator is used to determine the order. If the specified comparator is null, then the returned comparator considers all non-null values to be equal.
        Type Parameters:
        T - the type of the elements to be compared
        Parameters:
        comparator - a SerializableComparator for comparing non-null values
        Returns:
        a comparator that considers null to be less than non-null, and compares non-null objects with the supplied SerializableComparator.
        Since:
        1.8
      • nullsLast

        static <T> SerializableComparator<T> nullsLast​(SerializableComparator<? super T> comparator)
        Returns a null-friendly comparator that considers null to be greater than non-null. When both are null, they are considered equal. If both are non-null, the specified Comparator is used to determine the order. If the specified comparator is null, then the returned comparator considers all non-null values to be equal.
        Type Parameters:
        T - the type of the elements to be compared
        Parameters:
        comparator - a SerializableComparator for comparing non-null values
        Returns:
        a comparator that considers null to be greater than non-null, and compares non-null objects with the supplied SerializableComparator.
        Since:
        1.8
      • comparing

        static <T,​U> SerializableComparator<T> comparing​(SerializableFunction<? super T,​? extends U> keyExtractor,
                                                               SerializableComparator<? super U> keyComparator)
        Accepts a function that extracts a sort key from a type T, and returns a SerializableComparator<T> that compares by that sort key using the specified SerializableComparator.

        The returned comparator is serializable.

        Type Parameters:
        T - the type of element to be compared
        U - the type of the sort key
        Parameters:
        keyExtractor - the function used to extract the sort key
        keyComparator - the SerializableComparator used to compare the sort key
        Returns:
        a comparator that compares by an extracted key using the specified SerializableComparator
        Throws:
        NullPointerException - if either argument is null
        Since:
        1.8
        API Note:
        For example, to obtain a SerializableComparator that compares Person objects by their last name ignoring case differences,
        
             SerializableComparator<Person> cmp = SerializableComparator.comparing(
                     Person::getLastName,
                     String.CASE_INSENSITIVE_ORDER);
         
      • comparing

        static <T,​U extends Comparable<? super U>> SerializableComparator<T> comparing​(SerializableFunction<? super T,​? extends U> keyExtractor)
        Accepts a function that extracts a Comparable sort key from a type T, and returns a Comparator<T> that compares by that sort key.
        Type Parameters:
        T - the type of element to be compared
        U - the type of the Comparable sort key
        Parameters:
        keyExtractor - the function used to extract the Comparable sort key
        Returns:
        a comparator that compares by an extracted key
        Throws:
        NullPointerException - if the argument is null
        Since:
        1.8
        API Note:
        For example, to obtain a SerializableComparator that compares Person objects by their last name,
        
             Comparator<Person> byLastName = Comparator.comparing(Person::getLastName);
         
      • comparingInt

        static <T> SerializableComparator<T> comparingInt​(SerializableToIntFunction<? super T> keyExtractor)
        Accepts a function that extracts an int sort key from a type T, and returns a Comparator<T> that compares by that sort key.
        Type Parameters:
        T - the type of element to be compared
        Parameters:
        keyExtractor - the function used to extract the integer sort key
        Returns:
        a comparator that compares by an extracted key
        Throws:
        NullPointerException - if the argument is null
        Since:
        1.8
        See Also:
        comparing(SerializableFunction)
      • comparingLong

        static <T> SerializableComparator<T> comparingLong​(SerializableToLongFunction<? super T> keyExtractor)
        Accepts a function that extracts a long sort key from a type T, and returns a Comparator<T> that compares by that sort key.
        Type Parameters:
        T - the type of element to be compared
        Parameters:
        keyExtractor - the function used to extract the long sort key
        Returns:
        a comparator that compares by an extracted key
        Throws:
        NullPointerException - if the argument is null
        Since:
        1.8
        See Also:
        comparing(SerializableFunction)
      • comparingDouble

        static <T> SerializableComparator<T> comparingDouble​(SerializableToDoubleFunction<? super T> keyExtractor)
        Accepts a function that extracts a double sort key from a type T, and returns a SerializableComparator<T> that compares by that sort key.
        Type Parameters:
        T - the type of element to be compared
        Parameters:
        keyExtractor - the function used to extract the double sort key
        Returns:
        a comparator that compares by an extracted key
        Throws:
        NullPointerException - if the argument is null
        Since:
        1.8
        See Also:
        comparing(SerializableFunction)