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

(coll) Invoking tailSet() on a TreeSet with null

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • P4
    • None
    • 6
    • core-libs

    Description

      FULL PRODUCT VERSION :
      java version "1.6.0"
      Java(TM) SE Runtime Environment (build 1.6.0-b105)
      Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP
      Professional
      Version 2002
      Service Pack 2

      A DESCRIPTION OF THE PROBLEM :
      A TreeSet instance is constructed with a Comparator that sorts null values in the end of the set.

      Five elements are added to the set, where one of them is null.

      Another sorted set is created from the first one by tailSet(). The null value should be included in this new sorted set.

      Unfortunately this null value is not visible using some investigating methods, but visible with others.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the code with:
      javac TestTailSet.java

      Run it with:
      java TestTailSet

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The out from the compilation:
      Note: TestTailSet.java uses unchecked or unsafe operations.
      Note: Recompile with -Xlint:unchecked for details.

      This is expected and correct.

      When running the code I expect that:
      The printout of the tail set will contain null. That is invocation of toString().
      The size of the tail set is 3.
      When printing the tail set element by element using a loop would print out null.

      ACTUAL -
      This is the actual printout from the code:
      Print set: [1, 2, 3, 4, null]. Ok!
      Print tailSet: [3, 4]. The null seems to be missing.
      Does set contain null: [true]. Ok!
      Does tailSet contain null: [true]. Ok!
      Print last element in set: [null]. Ok!
      Print last element in tailSet: [null]. Ok!
      Print size of set: [5]. Ok!
      Print size of tailSet: [2]. Expected value 3.
      Print element by element in set using a loop: [ 1 2 3 4 null ]. Ok!
      Print element by element in tailSet using a loop: [ 3 4 ]. The null seems to be missing.


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.Arrays;
      import java.util.Comparator;
      import java.util.SortedSet;
      import java.util.TreeSet;

      class TestTailSet {

      private static void testTailSet() {
      TreeSet<String> set = new TreeSet<String>(new Comparator() {
      public int compare (Object o1, Object o2) {
      if (o1 == null && o2 == null) return 0;
      else if (o1 != null && o2 == null) return -1;
      else if (o1 == null && o2 != null) return 1;
      else if (o1 != null && o2 != null) return ((Comparable)o1).compareTo(o2);
      else throw new IllegalStateException();
      }
      });
      set.addAll(Arrays.asList("1","2","3","4",null));
      SortedSet<String> tailSet = set.tailSet("3");
      System.out.println("Print set: " + set + ". Ok!");
      System.out.println("Print tailSet: " + tailSet + ". The null seems to be missing.");
      System.out.println("Does set contain null: [" + set.contains(null) + "]. Ok!");
      System.out.println("Does tailSet contain null: [" + tailSet.contains(null) + "]. Ok!");
      System.out.println("Print last element in set: [" + set.last() + "]. Ok!");
      System.out.println("Print last element in tailSet: [" + tailSet.last()+ "]. Ok!");
      System.out.println("Print size of set: [" + set.size() + "]. Ok!");
      System.out.println("Print size of tailSet: [" + tailSet.size() + "]. Expected value 3.");
      System.out.print("Print element by element in set using a loop: [");
      for (String o : set) System.out.print(" " + o); System.out.println(" ]. Ok!");
      System.out.print("Print element by element in tailSet using a loop: [");
      for (String o : tailSet) System.out.print(" " + o); System.out.println(" ]. The null seems to be missing.");
      }

      public static void main (String[] args) {
      testTailSet();
      }
      }
      ---------- END SOURCE ----------

      Attachments

        Issue Links

          Activity

            People

              martin Martin Buchholz
              ryeung Roger Yeung (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: