Java中EnumMap的entrySet()方法: Java.util.EnumMap.entrySet()

作者: Arvin Chen 分类: Java 来源: Break易站(www.breakyizhan.com)

Java中的Java.util.EnumMap.entrySet()方法用于创建EnumMap中包含的元素集。它基本上返回枚举映射的设置视图。

句法:

enum_map.entrySet()

参数:该方法不带任何参数。

返回值:该方法返回此enum_map中包含的映射的set视图。

下面的程序说明了Java.util.EnumMap.entrySet()方法

程序1:

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

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

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

        EnumMap<rank_countries, Integer> mp = new
        EnumMap<rank_countries, Integer>(rank_countries.class);

        // Values are associated in mp
        mp.put(rank_countries.India, 72);
        mp.put(rank_countries.United_States, 1083);
        mp.put(rank_countries.China, 4632);
        mp.put(rank_countries.Japan, 6797);

        // Map view
        System.out.println("Map view: " + mp);

        // Creating a new set of the mappings
        // contained in map
        Set<Map.Entry<rank_countries, Integer> > set_view =
                                                mp.entrySet();

        // Set view of the mappings
        System.out.println("Set view of the map: " + set_view);
    }
}

输出:

Map view: {India=72, United_States=1083, China=4632, Japan=6797}
Set view of the map: [India=72, United_States=1083, China=4632, Japan=6797]

程序2:

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

// An enum of gfg ranking worldwide and in india
public enum gfg {
    Global_2018,
    India_2018
};

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

        EnumMap<gfg, Integer> mp = new
              EnumMap<gfg, Integer>(gfg.class);

        // Values are associated in mp
        mp.put(gfg.Global_2018, 800);
        mp.put(gfg.India_2018, 72);

        // Mapping view
        System.out.println("Mapping view: " + mp);

        // Creating a new set of the mappings
        Set<Map.Entry<gfg, Integer> > set_view =
                                    mp.entrySet();

        // Set view of the mappings
        System.out.println("Set view of the map: "
                                      + set_view);
    }
}

输出:

Mapping view: {Global_2018=800, India_2018=72}
Set view of the map: [Global_2018=800, India_2018=72]
  •   本文标题:Java中EnumMap的entrySet()方法: Java.util.EnumMap.entrySet() - Break易站
    转载请保留页面地址:https://www.breakyizhan.com/java/5450.html
    扫描二维码添加微信 
  • ,领取淘宝优惠券,淘宝购物更优惠。现在添加微信,还可以领取机械键盘优惠券!添加微信后,分享淘宝选中的机械键盘给淘宝机器人即可领取!
    支持我们,就用微信淘宝!

    发表笔记

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

    更多阅读