Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8184694

add lexicographic comparator methods for collections

    Details

    • Type: Enhancement
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:

      Description

      There should be methods that compare two collections lexicographically, something like this on the Collections class:

      public static <T> int compare(Collection<? extends T> c1,
                                        Collection<? extends T> c2,
                                        Comparator<? super T> cmp)

      public static <T extends Comparable<? super T>>
          int compare(Collection<? extends T> c1, Collection<? extends T> c2)

      Of course, the collections must have a well-defined iteration order, such as List, LinkedHashSet, or SortedSet.

        Issue Links

          Activity

          Hide
          smarks Stuart Marks added a comment -
          It's unclear whether it's necessary to have combinator forms of these methods that *return* Comparators as opposed to methods that *are* Comparators. For example, we might have:

              public static <T> Comparator<Collection<? extends T>> comparator(Comparator<? super T> downstream)
              public static <T extends Comparable<? super T>> Comparator<Collection<? extends T>> comparator()

          This would allow one to use downstream comparators somewhat more easily, e.g.

              new TreeSet<>(Collections.comparator(downstream))

          instead of

              new TreeSet<>((a, b) -> Collections.compare(a, b, downstream))
          Show
          smarks Stuart Marks added a comment - It's unclear whether it's necessary to have combinator forms of these methods that *return* Comparators as opposed to methods that *are* Comparators. For example, we might have:     public static <T> Comparator<Collection<? extends T>> comparator(Comparator<? super T> downstream)     public static <T extends Comparable<? super T>> Comparator<Collection<? extends T>> comparator() This would allow one to use downstream comparators somewhat more easily, e.g.     new TreeSet<>(Collections.comparator(downstream)) instead of     new TreeSet<>((a, b) -> Collections.compare(a, b, downstream))

            People

            • Assignee:
              smarks Stuart Marks
              Reporter:
              smarks Stuart Marks
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: