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

Clarify Map.Entry's connection to the underlying map

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.7.0_51"
      Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
      Java HotSpot(TM) Client VM (build 24.51-b03, mixed mode, sharing)


      A DESCRIPTION OF THE PROBLEM :
      The javadoc for java.util.TreeMap states:

      "All Map.Entry pairs returned by methods in this class and its views represent snapshots of mappings at the time they were produced. They do not support the Entry.setValue method. (Note however that it is possible to change mappings in the associated map using put.)"

      However Entry.setValue is supported, and does change the underlying map, as demonstrated by the attached test case.


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.Map.Entry;
      import java.util.TreeMap;
      import org.junit.Test;
      import static org.junit.Assert.*;

      public class TreeMapEntrySetValueTest
      {
          @Test
          public void setValueTest()
          {
              final String key = "key";
              final String initialValue = "initialvalue";
              final String newValue = "newvalue";
              
              TreeMap<String, String> map = new TreeMap<String, String>();
              map.put(key, initialValue);
              for (Entry<String, String> e : map.entrySet())
              {
                  e.setValue(newValue);
              }
              
              assertEquals(newValue, map.get(key));
          }
      }
      ---------- END SOURCE ----------

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

              Created:
              Updated:
              Resolved: