Java中EnumMap的get(Object key)方法: get(Object key)

作者: Arvin Chen 分类: Java 来源: Break易站(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%
  •   本文标题:Java中EnumMap的get(Object key)方法: get(Object key) - Break易站
    转载请保留页面地址:https://www.breakyizhan.com/java/5455.html

    发表笔记

    电子邮件地址不会被公开。 必填项已用*标注

    更多阅读