java.util.EnumMap.get() 方法返回指定键映射到的值,如果是则返回 null该映射不包含该键的映射。
返回值 null 并不一定表示该映射不包含该键的映射;映射也可能显式地将键映射为 null。
语法
public V get(Object key)
这里,V是容器维护的值的类型。
参数
key | 指定要返回关联值的key。 |
返回值
返回指定键映射到的值,如果此映射不包含该键的映射,则返回 null。
异常
无示例:
在下面的示例中,java.util.EnumMap.get() 方法返回给定键中映射到的值枚举映射。
import java.util.*;
public class MyClass {
//创建一个枚举
public enum weekday{
MON, TUE, WED, THU, FRI
}
public static void main(String[] args) {
//创建EnumMap
EnumMap<weekday,Integer> MyMap =
new EnumMap<weekday,Integer>(weekday.class);
//关联MyMap中的值
MyMap.put(weekday.MON, 1);
MyMap.put(weekday.TUE, 2);
MyMap.put(weekday.WED, 3);
MyMap.put(weekday.THU, 4);
MyMap.put(weekday.FRI, 5);
//打印指定键的映射值
System.out.println("MON is mapped to: " + MyMap.get(weekday.MON));
System.out.println("WED is mapped to: " + MyMap.get(weekday.WED));
System.out.println("FRI is mapped to: " + MyMap.get(weekday.FRI));
}
}
上述代码的输出将是:
MON is mapped to: 1
WED is mapped to: 3
FRI is mapped to: 5