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

TreeMap.entrySet.iterator.remove corrupts the last returned entry

    Details

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_144"
      Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
      Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows 10 build 15063.632

      A DESCRIPTION OF THE PROBLEM :
      For a TreeMap entrySet iterator, the last Map.Entry returned by next() is updated to contain the key and value for the upcoming entry when calling remove() on all but the first or last elements in the iterator.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Use the test case source code to create a TreeMap with 3 entries, and call i.remove() on the middle one

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      before b
      after b

      ACTUAL -
      before b
      after c


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.Iterator;
      import java.util.Map;
      import java.util.TreeMap;

      public class Bug
      {
      public static void main(String... args)
      {
      Map<String, String> x = new TreeMap<>();
      x.put("a", "1");
      x.put("b", "2");
      x.put("c", "3");
      Iterator<Map.Entry<String, String>> i = x.entrySet().iterator();
      Map.Entry<String, String> y = i.next();
      y = i.next();
      System.out.println("before " + y.getKey());
      i.remove();
      System.out.println("after " + y.getKey());
      }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Consciously avoiding using .remove() before using the entry.getKey() or entry.getValue() for entryset iterators - if a map is passed to other function, you can not guarantee it is not a TreeMap...

        Attachments

          Activity

            People

            • Assignee:
              dl Doug Lea
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated: