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

TreeMap.DescendingKeyIterator 'remove' confuses iterator position

    XMLWordPrintable

Details

    • Bug
    • Status: Closed
    • P3
    • Resolution: Fixed
    • 6-pool, 7u25
    • 8
    • core-libs

    Description

      FULL PRODUCT VERSION :
      Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)


      A DESCRIPTION OF THE PROBLEM :
      The implementation of 'remove' in PrivateEntryIterator in TreeMap.java does not work correctly when it is a descending iterator.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Put at least 3 elements into a TreeMap.
      2. Get a descending iterator for a TreeMap's KeySet.
      3. Iterate through the elements, and remove the second one.

      The iterator position is now wrong. (See the test case below for source code.)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

      import java.util.ArrayList;
      import java.util.Iterator;
      import java.util.List;
      import java.util.NavigableMap;
      import java.util.TreeMap;

      public class TreeMapIteratorBug {
        public static void main(String [] argv) throws Exception {
          NavigableMap<String, Object> map = new TreeMap<String, Object>();
          map.put("a", 1);
          map.put("b", 1);
          map.put("c", 1);

          List<String> keys = new ArrayList<String>();
          keys.addAll(map.descendingMap().keySet());

          Iterator<String> iterator = map.navigableKeySet().descendingIterator();
          for (int i = 0; i < 3; i++) {
            String key = iterator.next();
            if (!key.equals(keys.get(i))) {
              throw new Exception("bug");
            }
            if (i == 1) {
              iterator.remove();
            }
          }
        }
      }

      ---------- END SOURCE ----------

      Attachments

        Activity

          People

            bchristi Brent Christian
            coffeys Sean Coffey
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: