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

(coll) head/tail submaps of TreeMaps discard null-keyed entry

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • 8
    • 6u21
    • core-libs

      FULL PRODUCT VERSION :
      java version "1.6.0_21"
      Java(TM) SE Runtime Environment (build 1.6.0_21-b07)
      Java HotSpot(TM) 64-Bit Server VM (build 17.0-b17, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7600]

      A DESCRIPTION OF THE PROBLEM :
      Children of TreeMaps constructed with a null-aware comparator fail to account for null keys. TreeMap.SubMapIterator assumes that a null key signals that the last entry has been processed. In some cases, this leads to premature termination. See also bugs 4286765, 5018254 and 6467933.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Construct TreeSet with comparator that places nulls at end.
      2. Add several elements to set.
      3. Add null to set.
      4. Inspect tailSet.toString()

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Set should include null.
      ACTUAL -
      Set does not include null.

      REPRODUCIBILITY :
      This bug can be reproduced always.

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

      public class Test implements Comparator<Character> {
        public int compare(Character pChar1, Character pChar2) {
          return
           pChar1 == null ?
           (pChar2 == null ? 0 : 1) :
           (pChar2 == null ? -1 : pChar1.compareTo(pChar2));
        }

        public static void main(String[] pArgs) {
          final TreeSet<Character> set = new TreeSet<Character>(new Test());
          set.addAll(Arrays.asList('a', 'b', 'c', null));
          set.add(null);
          System.out.println(set.size());
          System.out.println(set);
          System.out.println(set.tailSet('a', false).size());
          System.out.println(set.tailSet('a', false));
        }
      }
      ---------- END SOURCE ----------

            dmocek Darryl Mocek
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: