Java EnumMap get()方法
Java.util.EnumMap.get( Object key )方法是用来给出指定键的映射值的。
语法
get(Object key)
参数: 该方法需要一个参数key,通过它来返回与之相关的值。
返回值: 它返回与相关键映射的值。
下面的程序说明了get()函数的工作原理。
程序1:
// Java program to demonstrate get(key)
import java.util.*;
// An enum of geeksforgeeks
public enum gfg {
Global,
India
}
;
class Enum_demo {
public static void main(String[] args)
{
EnumMap<gfg, Integer> mp = new
EnumMap<gfg, Integer>(gfg.class);
// Values are associated
mp.put(gfg.Global, 800);
mp.put(gfg.India, 72);
// Stores the value mapped with the key specified
int res = mp.get(gfg.India);
// Prints the result
System.out.println("GeeksforGeeks ranked in India: "
+ res);
}
}
输出:
GeeksforGeeks ranked in India: 72
程序2
// Java program to demonstrate get(key)
import java.util.*;
// An enum of geeksforgeeks
public enum gfg {
India,
United_States,
China
}
;
class Enum_demo {
public static void main(String[] args)
{
EnumMap<gfg, String> mp = new
EnumMap<gfg, String>(gfg.class);
// Values are associated
mp.put(gfg.India, "61.7%");
mp.put(gfg.United_States, "18.2%");
mp.put(gfg.China, "2.5%");
// Stores the value mapped with the key specified
String res = mp.get(gfg.United_States);
// Prints the result
System.out.println("Visitors in United_States: "
+ res);
}
}
输出:
Visitors in United_States: 18.2%