Java中EnumMap的containsKey(key)方法: containsKey(key)

作者: Arvin Chen 分类: Java 发布时间: 2018-08-31 21:39

Java.util.EnumMap.containsKey(key)方法用于检查参数中提到的指定键是否存在于此映射中。

句法:

boolean containsKey(Object key)

参数:该方法接受一个参数密钥,该密钥指向要验证的密钥。

返回值:如果密钥存在于EnumMap中,则该方法返回true ,否则返回false。

下面的程序说明了containsKey() 方法

Java.util.EnumMap.containsKey(key)程序1:

// Java program to demonsrate containsKey() method
import java.util.*;

// An enum of gfg visitors is created
public enum gfg_visitors {
    India,
    United_States,
    China,
    Japan,
    Canada
};

class Enum_map {
    public static void main(String[] args)
    {

        EnumMap<gfg_visitors, String> mp = new
        EnumMap<gfg_visitors, String>(gfg_visitors.class);

        // values are associated in mp
        mp.put(gfg_visitors.India, "61.4%");
        mp.put(gfg_visitors.United_States, "18.4%");
        mp.put(gfg_visitors.China, "2.5%");
        mp.put(gfg_visitors.Japan, "1.1%");
        mp.put(gfg_visitors.Canada, "1.1%");

        // Check if map contains gfg visitor from United_States
        boolean ans = mp.containsKey(gfg_visitors.United_States);

        // Prints the result
        System.out.println("gfg_visitors from United States: " + ans);
    }
}

输出:

gfg_visitors from United States: true

Java.util.EnumMap.containsKey(key)程序2:

// Java program to demonsrate containsKey() method
import java.util.*;

// An enum of gfg visitors is created
public enum gfg_visitors {
    India,
    United_States,
    China,
    Japan,
    Canada,
    Russia
};

class Enum_map {
    public static void main(String[] args)
    {

        EnumMap<gfg_visitors, String> mp = new
        EnumMap<gfg_visitors, String>(gfg_visitors.class);

        // values are associated in mp
        mp.put(gfg_visitors.India, "61.4%");
        mp.put(gfg_visitors.United_States, "18.4%");
        mp.put(gfg_visitors.China, "2.5%");
        mp.put(gfg_visitors.Japan, "1.1%");
        mp.put(gfg_visitors.Canada, "1.1%");

        // check if map contains gfg visitor from Russia
        boolean ans = mp.containsKey(gfg_visitors.Russia);

        // prints the result
        System.out.println("gfg_visitors from Russia: " + ans);
    }
}

输出:

gfg_visitors from Russia: false
  •   本文标题:Java中EnumMap的containsKey(key)方法: containsKey(key) - Break易站
    转载请保留页面地址:https://www.breakyizhan.com/java/5453.html
    扫描二维码添加微信 
  • ,领取淘宝优惠券,淘宝购物更优惠。现在添加微信,还可以领取机械键盘优惠券!添加微信后,分享淘宝选中的机械键盘给淘宝机器人即可领取!
    支持我们,就用微信淘宝!

    发表笔记

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

    更多阅读