A specialized Map implementation for use with enum type keys. All of the keys in an enum map must come from a single enum type that is specified, explicitly or implicitly, when the map is created. Enum maps are represented internally as arrays. This representation is extremely compact and efficient.
Enum maps are maintained in the natural order of their keys (the order in which the enum constants are declared). This is reflected in the iterators returned by the collections views (keySet(), entrySet(), and values()).
Iterators returned by the collection views are weakly consistent: they will never throw ConcurrentModificationException and they may or may not show the effects of any modifications to the map that occur while the iteration is in progress.
package collection.demos;
import java.util.EnumMap;
public class EnumMapDemo {
enum keys {
k1, k2, k3;
}
public static void main(String[] args) {
EnumMap<keys, String> m = new EnumMap<keys, String>(keys.class);
m.put(keys.k1, "AAA");
m.put(keys.k2,"BBB");
m.put(keys.k3, "CCC");
System.out.println("EnumMap : "+m);
System.out.println("Value of k2 "+m.get(keys.k2));
}
}
Enum maps are maintained in the natural order of their keys (the order in which the enum constants are declared). This is reflected in the iterators returned by the collections views (keySet(), entrySet(), and values()).
Iterators returned by the collection views are weakly consistent: they will never throw ConcurrentModificationException and they may or may not show the effects of any modifications to the map that occur while the iteration is in progress.
package collection.demos;
import java.util.EnumMap;
public class EnumMapDemo {
enum keys {
k1, k2, k3;
}
public static void main(String[] args) {
EnumMap<keys, String> m = new EnumMap<keys, String>(keys.class);
m.put(keys.k1, "AAA");
m.put(keys.k2,"BBB");
m.put(keys.k3, "CCC");
System.out.println("EnumMap : "+m);
System.out.println("Value of k2 "+m.get(keys.k2));
}
}
Comments