📜  Java中的 EnumMap get() 方法

📅  最后修改于: 2022-05-13 01:55:43.626000             🧑  作者: Mango

Java中的 EnumMap get() 方法

Java中的Java .util.EnumMap.get( Object key ) 方法用于给出指定键的映射值。

句法:

get(Object key)

参数:该方法采用一个参数,该键被传递以返回与其关联的值。

返回值:它返回与关联键映射的值。

下面的程序说明了 get()函数的工作:

方案一:

// 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 mp = new 
                     EnumMap(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

方案二:

// 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 mp = new 
                  EnumMap(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%