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

EnumMap error msg could be more helpful when you attempt to access removed entry

XMLWordPrintable

      The following program throws IllegalStateException. There is nothing wrong with this behavior, but it is a bit confusing.

      There is no guarantee that calling getKey or getValue will work on an entry once it is removed. The behavior of EnumMap is identical to the behavior of IndentityHashMap in this regard. In both cases, the maps use the same object as an iterator and entries, which necessitates certain compromises.

      To lessen the confusion, the IllegalStateException should have a detail message that says "Entry was removed" or words to that effect. (This is what IdentityHashMap does.)



      import java.util.*;

      public class test {
          public static void main(String[] argv) {
              EnumMap map = new EnumMap(simpleEnum.class);
              Iterator iter = null;
              Map.Entry entry = null;
              map.put(simpleEnum.FIRST, "First");
              map.put(simpleEnum.LAST, "Last");
              iter = map.entrySet().iterator();
              while (iter.hasNext()) {
                  entry = (Map.Entry)(iter.next());
                  iter.remove();
                  System.out.println("ContainsKey: "+map.containsKey(entry.getKey()));
              };
          }
      }

      enum simpleEnum { FIRST, LAST }

            jjb Josh Bloch
            jjb Josh Bloch
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: