Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8020146 JCK issues for java.util.stream
  3. JDK-8020009

TreeMap.entrySet().spliterator() reports SORTED + null comparator but the elements are not Comparable

    XMLWordPrintable

Details

    Description

      The spec
      http://download.java.net/jdk8/docs/api/java/util/Spliterator.html#SORTED

      says:

      "Characteristic value signifying that encounter order follows a defined sort order. If so, method getComparator() returns the associated Comparator, or null if all elements are Comparable and are sorted by their natural ordering."

      This assertion is violated by TreeMap.entrySet().spliterator(). Please see the following code:

              final Spliterator spliterator = new TreeMap() {{
                  put("a", "a");
                  put("b", "b");
              }}.entrySet().spliterator();

              System.out.println("spliterator.hasCharacteristics(Spliterator.SORTED) = " + spliterator.hasCharacteristics(Spliterator.SORTED));
              System.out.println("spliterator.getComparator() = " + spliterator.getComparator());

              spliterator.tryAdvance(e -> {Comparable c = (Comparable)e;});


      The output will be:

      spliterator.hasCharacteristics(Spliterator.SORTED) = true
      spliterator.getComparator() = null
      Exception in thread "main" java.lang.ClassCastException: java.util.TreeMap$Entry cannot be cast to java.lang.Comparable
      at TreeMapEntrySet.lambda$0(TreeMapEntrySet.java:1)
      at TreeMapEntrySet$$Lambda$1.accept(Unknown Source)
      at java.util.TreeMap$EntrySpliterator.tryAdvance(TreeMap.java:2933)
      at TreeMapEntrySet.main(TreeMapEntrySet.java:18)



      The following JCK tests will fail due to this:

      api/java_util/Collections/unmodifiable/index.html#UnmodifiableSortedMapEntrySetSpliterator[getComparator_SORTED_naturally_comparingElements]
      api/java_util/TreeMap/EntrySetSpliterator.html#EntrySetSpliterator[getComparator_SORTED_naturally_comparingElements]
      api/java_util/TreeMap/EntrySetParallelStream.html#EntrySetParallelStream[checkSpliterator]
      api/java_util/TreeMap/EntrySetStream.html#EntrySetStream[checkSpliterator]
      api/java_util/Collections/synchronizd/index.html#SynchronizedSortedMapEntrySetSpliterator[getComparator_SORTED_naturally_comparingElements]
      api/java_util/Collections/synchronizd/index.html#SynchronizedSortedMapEntrySetStream[checkSpliterator]
      api/java_util/Collections/synchronizd/index.html#SynchronizedSortedMapEntrySetParallelStream[checkSpliterator]






      Attachments

        Activity

          People

            psandoz Paul Sandoz
            dbessono Dmitry Bessonov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: