當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java EnumMap get()用法及代碼示例


Java中的Java.util.EnumMap.get(Object key)方法用於提供指定鍵的映射值。

用法:

get(Object 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%


相關用法


注:本文由純淨天空篩選整理自akash1295大神的英文原創作品 EnumMap get() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。