• Tags
  •         
  • www.breakyizhan.com
  •    

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

    句法:

    get(Object key)

    参数:该方法接受一个参数,该被传递以返回与之关联的值。

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

    下面的程序说明了Java.util.EnumMap.get(Object key)函数的工作原理

    Java.util.EnumMap.get(Object key)程序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

    Java.util.EnumMap.get(Object key)程序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%

     
    转载请保留页面地址:https://www.breakyizhan.com/java/5455.html