Java EnumMap entrySet()

entrySet() 用于获取EnumMap的所有Entry(key-vlaue)的Set集合。

1 语法

public Set<Map.Entry<K, V>> entrySet()  

2 参数

3 返回值

返回所有Entry(key-vlaue)的Set集合。

4 EnumMap entrySet()示例1

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java EnumMap.entrySet的例子
 */
import java.util.*;

public class Demo {

    public enum Tutorial {
        CSS, Python, PHP, Java, Javascript
    };
    public static void main(String[] args) {
        EnumMap<Tutorial, String>  map =
                new EnumMap<Tutorial, String> (Tutorial.class);

        map.put(Tutorial.CSS, "1");
        map.put(Tutorial.Python, "2");
        map.put(Tutorial.PHP, "3");
        map.put(Tutorial.Java, "4");
        //输出Map
        System.out.println("Map: "+map);
        Set<Map.Entry<Tutorial, String> > set = map.entrySet();
        //输出Set
        System.out.println("Set: "+set);
    }
}

输出结果为:

Map: {CSS=1, Python=2, PHP=3, Java=4}
Set: [CSS=1, Python=2, PHP=3, Java=4]

5 EnumMap entrySet()示例2

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java EnumMap.entrySet的例子
 */
import java.util.*;

public class Demo {

    // create an enum
    public enum Days {
        Monday, Tuesday, Wednesday, Thursday
    };
    public static void main(String[] args) {
        //创建EnumMap
        EnumMap<Days, String> map= new EnumMap<Days, String>(Days.class);

        map.put(Days.Monday,"1");
        map.put(Days.Tuesday,"2");
        map.put(Days.Wednesday,"3");
        map.put(Days.Thursday,"4");
        // 输出Map
        System.out.println("Map: "+map);
        Set<Map.Entry<Days, String> > set = map.entrySet();
        // 输出Set
        System.out.println("Set: "+set);
    }
}

输出结果为:

Map: {Monday=1, Tuesday=2, Wednesday=3, Thursday=4}
Set: [Monday=1, Tuesday=2, Wednesday=3, Thursday=4]

 

热门文章

优秀文章