优点:常量做为Key,在编译期就确定了。Enum做为key,在运行时也可以改变

package enumdemo;

import java.util.EnumMap;
import java.util.Map;
import java.util.Set; public class EnumMaps {
public static void main(String[] args) {
EnumMap<AlarmPoints, Command> em = new EnumMap<AlarmPoints, Command>(AlarmPoints.class);
em.put(AlarmPoints.KITCHEN, new Command() {
@Override
public void action() {
System.out.println("action:kitchen...");
}
});
em.put(AlarmPoints.BATHROOM, new Command() {
@Override
public void action() {
System.out.println("action:bathroom");
}
}); Set<Map.Entry<AlarmPoints, Command>> set = em.entrySet();
for (Map.Entry<AlarmPoints, Command> entry : set) {
System.out.println(entry.getKey() + "");
entry.getValue().action();
} System.out.println(em.get(AlarmPoints.UTILITY) + ""); }
} enum AlarmPoints {
KITCHEN,
BATHROOM,
UTILITY
} interface Command {
void action();
}
05-04 11:31