-
Bug
-
Resolution: Fixed
-
P3
-
1.4.0
-
b40
-
generic
-
generic
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 }
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 }