Uses of Interface java.util.Comparator (Java Platform SE 8 )
Date: 2026-04-17

## Uses of Interface
java.util.Comparator

- Packages that use Comparator
  Package | Description
  --- | ---
  java.lang | Provides classes that are fundamental to the design of the Java programming language.
  java.text | Provides classes and interfaces for handling text, dates, numbers, and messages in a manner independent of natural languages.
  java.time | The main API for dates, times, instants, and durations.
  java.time.chrono | Generic API for calendar systems other than the default ISO.
  java.util | Contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-number generator, and a bit array).
  java.util.concurrent | Utility classes commonly useful in concurrent programming.
  java.util.function | Functional interfaces provide target types for lambda expressions and method references.
  java.util.stream | Classes to support functional-style operations on streams of elements, such as map-reduce transformations on collections.
  javax.swing | Provides a set of "lightweight" (all-Java language) components that, to the maximum degree possible, work the same on all platforms.
  javax.swing.table | Provides classes and interfaces for dealing with `javax.swing.JTable`.

- - ### Uses of Comparator in java.lang

    Fields in java.lang declared as Comparator
    Modifier and Type | Field and Description
    --- | ---
    `static Comparator<String>` | String.`CASE_INSENSITIVE_ORDER` A Comparator that orders `String` objects as by `compareToIgnoreCase`.

  - ### Uses of Comparator in java.text

    Classes in java.text that implement Comparator
    Modifier and Type | Class and Description
    --- | ---
    `class` | `Collator` The `Collator` class performs locale-sensitive `String` comparison.
    `class` | `RuleBasedCollator` The `RuleBasedCollator` class is a concrete subclass of `Collator` that provides a simple, data-driven, table collator.

  - ### Uses of Comparator in java.time

    Methods in java.time that return Comparator
    Modifier and Type | Method and Description
    --- | ---
    `static Comparator<OffsetDateTime>` | OffsetDateTime.`timeLineOrder()` Gets a comparator that compares two `OffsetDateTime` instances based solely on the instant.

  - ### Uses of Comparator in java.time.chrono

    Methods in java.time.chrono that return Comparator
    Modifier and Type | Method and Description
    --- | ---
    `static Comparator<ChronoZonedDateTime<?>>` | ChronoZonedDateTime.`timeLineOrder()` Gets a comparator that compares `ChronoZonedDateTime` in time-line order ignoring the chronology.
    `static Comparator<ChronoLocalDate>` | ChronoLocalDate.`timeLineOrder()` Gets a comparator that compares `ChronoLocalDate` in time-line order ignoring the chronology.
    `static Comparator<ChronoLocalDateTime<?>>` | ChronoLocalDateTime.`timeLineOrder()` Gets a comparator that compares `ChronoLocalDateTime` in time-line order ignoring the chronology.

  - ### Uses of Comparator in java.util

    Methods in java.util that return Comparator
    Modifier and Type | Method and Description
    --- | ---
    `Comparator<? super E>` | PriorityQueue.`comparator()` Returns the comparator used to order the elements in this queue, or `null` if this queue is sorted according to the natural ordering of its elements.
    `Comparator<? super K>` | TreeMap.`comparator()`
    `Comparator<? super E>` | TreeSet.`comparator()`
    `Comparator<? super K>` | SortedMap.`comparator()` Returns the comparator used to order the keys in this map, or `null` if this map uses the natural ordering of its keys.
    `Comparator<? super E>` | SortedSet.`comparator()` Returns the comparator used to order the elements in this set, or `null` if this set uses the natural ordering of its elements.
    `static <T,U extends Comparable<? super U>> Comparator<T>` | Comparator.`comparing(Function<? 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.
    `static <T,U> Comparator<T>` | Comparator.`comparing(Function<? super T,? extends U> keyExtractor, Comparator<? super U> keyComparator)` Accepts a function that extracts a sort key from a type `T`, and returns a `Comparator<T>` that compares by that sort key using the specified `Comparator`.
    `static <K extends Comparable<? super K>,V> Comparator<Map.Entry<K,V>>` | Map.Entry.`comparingByKey()` Returns a comparator that compares `Map.Entry` in natural order on key.
    `static <K,V> Comparator<Map.Entry<K,V>>` | Map.Entry.`comparingByKey(Comparator<? super K> cmp)` Returns a comparator that compares `Map.Entry` by key using the given `Comparator`.
    `static <K,V extends Comparable<? super V>> Comparator<Map.Entry<K,V>>` | Map.Entry.`comparingByValue()` Returns a comparator that compares `Map.Entry` in natural order on value.
    `static <K,V> Comparator<Map.Entry<K,V>>` | Map.Entry.`comparingByValue(Comparator<? super V> cmp)` Returns a comparator that compares `Map.Entry` by value using the given `Comparator`.
    `static <T> Comparator<T>` | Comparator.`comparingDouble(ToDoubleFunction<? super T> keyExtractor)` Accepts a function that extracts a `double` sort key from a type `T`, and returns a `Comparator<T>` that compares by that sort key.
    `static <T> Comparator<T>` | Comparator.`comparingInt(ToIntFunction<? 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.
    `static <T> Comparator<T>` | Comparator.`comparingLong(ToLongFunction<? 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.
    `default Comparator<? super T>` | Spliterator.`getComparator()` If this Spliterator's source is `Spliterator.SORTED` by a `Comparator`, returns that `Comparator`.
    `static <T extends Comparable<? super T>> Comparator<T>` | Comparator.`naturalOrder()` Returns a comparator that compares `Comparable` objects in natural order.
    `static <T> Comparator<T>` | Comparator.`nullsFirst(Comparator<? super T> comparator)` Returns a null-friendly comparator that considers `null` to be less than non-null.
    `static <T> Comparator<T>` | Comparator.`nullsLast(Comparator<? super T> comparator)` Returns a null-friendly comparator that considers `null` to be greater than non-null.
    `default Comparator<T>` | Comparator.`reversed()` Returns a comparator that imposes the reverse ordering of this comparator.
    `static <T> Comparator<T>` | Collections.`reverseOrder()` Returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the `Comparable` interface.
    `static <T extends Comparable<? super T>> Comparator<T>` | Comparator.`reverseOrder()` Returns a comparator that imposes the reverse of the natural ordering.
    `static <T> Comparator<T>` | Collections.`reverseOrder(Comparator<T> cmp)` Returns a comparator that imposes the reverse ordering of the specified comparator.
    `default Comparator<T>` | Comparator.`thenComparing(Comparator<? super T> other)` Returns a lexicographic-order comparator with another comparator.
    `default <U extends Comparable<? super U>> Comparator<T>` | Comparator.`thenComparing(Function<? super T,? extends U> keyExtractor)` Returns a lexicographic-order comparator with a function that extracts a `Comparable` sort key.
    `default <U> Comparator<T>` | Comparator.`thenComparing(Function<? super T,? extends U> keyExtractor, Comparator<? super U> keyComparator)` Returns a lexicographic-order comparator with a function that extracts a key to be compared with the given `Comparator`.
    `default Comparator<T>` | Comparator.`thenComparingDouble(ToDoubleFunction<? super T> keyExtractor)` Returns a lexicographic-order comparator with a function that extracts a `double` sort key.
    `default Comparator<T>` | Comparator.`thenComparingInt(ToIntFunction<? super T> keyExtractor)` Returns a lexicographic-order comparator with a function that extracts a `int` sort key.
    `default Comparator<T>` | Comparator.`thenComparingLong(ToLongFunction<? super T> keyExtractor)` Returns a lexicographic-order comparator with a function that extracts a `long` sort key.

    Methods in java.util with parameters of type Comparator
    Modifier and Type | Method and Description
    --- | ---
    `static <T> int` | Collections.`binarySearch(List<? extends T> list, T key, Comparator<? super T> c)` Searches the specified list for the specified object using the binary search algorithm.
    `static <T> int` | Arrays.`binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c)` Searches a range of the specified array for the specified object using the binary search algorithm.
    `static <T> int` | Arrays.`binarySearch(T[] a, T key, Comparator<? super T> c)` Searches the specified array for the specified object using the binary search algorithm.
    `static <T> int` | Objects.`compare(T a, T b, Comparator<? super T> c)` Returns 0 if the arguments are identical and `c.compare(a, b)` otherwise.
    `static <T,U> Comparator<T>` | Comparator.`comparing(Function<? super T,? extends U> keyExtractor, Comparator<? super U> keyComparator)` Accepts a function that extracts a sort key from a type `T`, and returns a `Comparator<T>` that compares by that sort key using the specified `Comparator`.
    `static <K,V> Comparator<Map.Entry<K,V>>` | Map.Entry.`comparingByKey(Comparator<? super K> cmp)` Returns a comparator that compares `Map.Entry` by key using the given `Comparator`.
    `static <K,V> Comparator<Map.Entry<K,V>>` | Map.Entry.`comparingByValue(Comparator<? super V> cmp)` Returns a comparator that compares `Map.Entry` by value using the given `Comparator`.
    `static <T> T` | Collections.`max(Collection<? extends T> coll, Comparator<? super T> comp)` Returns the maximum element of the given collection, according to the order induced by the specified comparator.
    `static <T> T` | Collections.`min(Collection<? extends T> coll, Comparator<? super T> comp)` Returns the minimum element of the given collection, according to the order induced by the specified comparator.
    `static <T> Comparator<T>` | Comparator.`nullsFirst(Comparator<? super T> comparator)` Returns a null-friendly comparator that considers `null` to be less than non-null.
    `static <T> Comparator<T>` | Comparator.`nullsLast(Comparator<? super T> comparator)` Returns a null-friendly comparator that considers `null` to be greater than non-null.
    `static <T> void` | Arrays.`parallelSort(T[] a, Comparator<? super T> cmp)` Sorts the specified array of objects according to the order induced by the specified comparator.
    `static <T> void` | Arrays.`parallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp)` Sorts the specified range of the specified array of objects according to the order induced by the specified comparator.
    `static <T> Comparator<T>` | Collections.`reverseOrder(Comparator<T> cmp)` Returns a comparator that imposes the reverse ordering of the specified comparator.
    `void` | ArrayList.`sort(Comparator<? super E> c)`
    `default void` | List.`sort(Comparator<? super E> c)` Sorts this list according to the order induced by the specified `Comparator`.
    `void` | Vector.`sort(Comparator<? super E> c)`
    `static <T> void` | Collections.`sort(List<T> list, Comparator<? super T> c)` Sorts the specified list according to the order induced by the specified comparator.
    `static <T> void` | Arrays.`sort(T[] a, Comparator<? super T> c)` Sorts the specified array of objects according to the order induced by the specified comparator.
    `static <T> void` | Arrays.`sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)` Sorts the specified range of the specified array of objects according to the order induced by the specified comparator.
    `default Comparator<T>` | Comparator.`thenComparing(Comparator<? super T> other)` Returns a lexicographic-order comparator with another comparator.
    `default <U> Comparator<T>` | Comparator.`thenComparing(Function<? super T,? extends U> keyExtractor, Comparator<? super U> keyComparator)` Returns a lexicographic-order comparator with a function that extracts a key to be compared with the given `Comparator`.

    Constructors in java.util with parameters of type Comparator
    Constructor and Description
    `PriorityQueue(Comparator<? super E> comparator)` Creates a `PriorityQueue` with the default initial capacity and whose elements are ordered according to the specified comparator.
    `PriorityQueue(int initialCapacity, Comparator<? super E> comparator)` Creates a `PriorityQueue` with the specified initial capacity that orders its elements according to the specified comparator.
    `TreeMap(Comparator<? super K> comparator)` Constructs a new, empty tree map, ordered according to the given comparator.
    `TreeSet(Comparator<? super E> comparator)` Constructs a new, empty tree set, sorted according to the specified comparator.

  - ### Uses of Comparator in java.util.concurrent

    Methods in java.util.concurrent that return Comparator
    Modifier and Type | Method and Description
    --- | ---
    `Comparator<? super E>` | ConcurrentSkipListSet.`comparator()`
    `Comparator<? super K>` | ConcurrentSkipListMap.`comparator()`
    `Comparator<? super E>` | PriorityBlockingQueue.`comparator()` Returns the comparator used to order the elements in this queue, or `null` if this queue uses the natural ordering of its elements.

    Methods in java.util.concurrent with parameters of type Comparator
    Modifier and Type | Method and Description
    --- | ---
    `void` | CopyOnWriteArrayList.`sort(Comparator<? super E> c)`

    Constructors in java.util.concurrent with parameters of type Comparator
    Constructor and Description
    `ConcurrentSkipListMap(Comparator<? super K> comparator)` Constructs a new, empty map, sorted according to the specified comparator.
    `ConcurrentSkipListSet(Comparator<? super E> comparator)` Constructs a new, empty set that orders its elements according to the specified comparator.
    `PriorityBlockingQueue(int initialCapacity, Comparator<? super E> comparator)` Creates a `PriorityBlockingQueue` with the specified initial capacity that orders its elements according to the specified comparator.

  - ### Uses of Comparator in java.util.function

    Methods in java.util.function with parameters of type Comparator
    Modifier and Type | Method and Description
    --- | ---
    `static <T> BinaryOperator<T>` | BinaryOperator.`maxBy(Comparator<? super T> comparator)` Returns a `BinaryOperator` which returns the greater of two elements according to the specified `Comparator`.
    `static <T> BinaryOperator<T>` | BinaryOperator.`minBy(Comparator<? super T> comparator)` Returns a `BinaryOperator` which returns the lesser of two elements according to the specified `Comparator`.

  - ### Uses of Comparator in java.util.stream

    Methods in java.util.stream with parameters of type Comparator
    Modifier and Type | Method and Description
    --- | ---
    `Optional<T>` | Stream.`max(Comparator<? super T> comparator)` Returns the maximum element of this stream according to the provided `Comparator`.
    `static <T> Collector<T,?,Optional<T>>` | Collectors.`maxBy(Comparator<? super T> comparator)` Returns a `Collector` that produces the maximal element according to a given `Comparator`, described as an `Optional<T>`.
    `Optional<T>` | Stream.`min(Comparator<? super T> comparator)` Returns the minimum element of this stream according to the provided `Comparator`.
    `static <T> Collector<T,?,Optional<T>>` | Collectors.`minBy(Comparator<? super T> comparator)` Returns a `Collector` that produces the minimal element according to a given `Comparator`, described as an `Optional<T>`.
    `Stream<T>` | Stream.`sorted(Comparator<? super T> comparator)` Returns a stream consisting of the elements of this stream, sorted according to the provided `Comparator`.

  - ### Uses of Comparator in javax.swing

    Methods in javax.swing that return Comparator
    Modifier and Type | Method and Description
    --- | ---
    `protected Comparator<? super Component>` | SortingFocusTraversalPolicy.`getComparator()` Returns the Comparator which will be used to sort the Components in a focus traversal cycle.
    `Comparator<?>` | DefaultRowSorter.`getComparator(int column)` Returns the `Comparator` for the specified column.

    Methods in javax.swing with parameters of type Comparator
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | SortingFocusTraversalPolicy.`setComparator(Comparator<? super Component> comparator)` Sets the Comparator which will be used to sort the Components in a focus traversal cycle.
    `void` | DefaultRowSorter.`setComparator(int column, Comparator<?> comparator)` Sets the `Comparator` to use when sorting the specified column.

    Constructors in javax.swing with parameters of type Comparator
    Constructor and Description
    `SortingFocusTraversalPolicy(Comparator<? super Component> comparator)` Constructs a SortingFocusTraversalPolicy with the specified Comparator.

  - ### Uses of Comparator in javax.swing.table

    Methods in javax.swing.table that return Comparator
    Modifier and Type | Method and Description
    --- | ---
    `Comparator<?>` | TableRowSorter.`getComparator(int column)` Returns the `Comparator` for the specified column.

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.