📅  最后修改于: 2023-12-03 15:16:02.871000             🧑  作者: Mango
Java.util.EnumMap类是Java集合框架中的一种实现Map接口的特殊映射实现,它仅允许将枚举类型用作其键。
public EnumMap(Class<K> keyType)
public EnumMap(EnumMap<K, ? extends V> m)
public EnumMap(Map<K, ? extends V> m)
public V put(K key, V value)
public V get(Object key)
public boolean containsKey(Object key)
public int size()
public Collection<V> values()
enum Week {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
public static void main(String[] args) {
EnumMap<Week, String> map = new EnumMap<>(Week.class);
map.put(Week.Monday, "星期一");
map.put(Week.Tuesday, "星期二");
map.put(Week.Wednesday, "星期三");
map.put(Week.Thursday, "星期四");
map.put(Week.Friday, "星期五");
map.put(Week.Saturday, "星期六");
map.put(Week.Sunday, "星期天");
System.out.println(map.get(Week.Monday));
System.out.println(map.containsKey(Week.Monday));
System.out.println(map.containsValue("星期日"));
System.out.println(map.size());
System.out.println(map.values());
}
以上代码输出结果为:
星期一
true
false
7
[星期一, 星期二, 星期三, 星期四, 星期五, 星期六, 星期天]