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

IdentityHashMap Entry Modifications Fail to Update Original Map

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      A bug exists in the IdentityHashMap implementation where modifications to a Map.Entry obtained from its entry set do not properly propagate back to the original map, breaking the expected bidirectional relationship between entries and their containing map.
      And this result is different with HashMap.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      true
      test=initial
      test=modified
      {test=modified}
      test=modified2
      {test=modified2}
      ACTUAL -
      true
      test=initial
      test=modified
      {test=initial}
      test=modified
      {test=modified2}

      ---------- BEGIN SOURCE ----------
              IdentityHashMap<String, String> mutMap = new IdentityHashMap<>();
              mutMap.put("test", "initial");
              Map.Entry<String, String> arrayEntry =
                  (Map.Entry<String, String>) mutMap.entrySet().toArray()[0];
              boolean flag = mutMap.entrySet().contains(arrayEntry);
              System.out.println(flag);
              System.out.println(arrayEntry);
              arrayEntry.setValue("modified");
              System.out.println(arrayEntry);
              System.out.println(mutMap);
              mutMap.put("test", "modified2");
              System.out.println(arrayEntry);
              System.out.println(mutMap);
      ---------- END SOURCE ----------

            smarks Stuart Marks
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: