Hi All:
When I develop EnumMap,I find EnumMap strange on RI. As the following code describes, the method entrySet() of EnumMap returns a set view of mappings contained in this map. Then we get the set's iterator and use the iterator's next() method to get an Entry which contains one mapping. But if we use next() method again to get another Entry, the previous Entry will also point to the next Entry.

import java.util.EnumMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import junit.framework.TestCase;

public class EnumMapTest extends TestCase{
   enum Size {
       Small, Middle, Big {
       };
   }
   public void test_entrySet() {
       EnumMap enumSizeMap = new EnumMap(Size.class);
       enumSizeMap.put(Size.Middle, 1);
       enumSizeMap.put(Size.Big, null);

       Set set = enumSizeMap.entrySet();
       Iterator iter = set.iterator();
       Map.Entry entry = (Map.Entry) iter.next();
       assertEquals(Size.Middle, entry.getKey());
       Map.Entry entry1 = (Map.Entry) iter.next();
       assertEquals(Size.Big, entry.getKey());
       assertSame(entry,entry1);
   }
}
I guess on RI, the returned iterator maintains a reference to current entry and returns this reference in iter.next() method. I do not think RI's behavior makes sense here. So I suggest not to follow RI on the behavior.

Best regards

--
Spark Shen
China Software Development Lab, IBM


---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to