Java EnumMap values()

values() 获取EnumMap所有value的Set集合。

1 语法

public Collection<V> values()  

2 参数

3 返回值

返回EnumMap所有value的Set集合。

4 EnumMap values()示例1

package com.yiidian;

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

public class Demo {

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

        map.put(Tutorial.Java, "1");
        map.put(Tutorial.CSS, "2");
        map.put(Tutorial.Python, "3");
        map.put(Tutorial.Android, "4");
        //输出Map
        System.out.println("Map: "+map);
        Collection<String>  values = map.values();
        //获取value对象
        System.out.println("Collection: "+values);
    }
}

输出结果为:

Map: {Java=1, CSS=2, Python=3, Android=4}
Collection: [1, 2, 3, 4]

5 EnumMap values()示例2

package com.yiidian;

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

public class Demo {

    //创建枚举
    public enum Days {
        Monday,Tuesday, Wednesday, Thursday
    };
    public static void main(String[] args) {
        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
        System.out.println("Map: "+map);
        //输出Map的元素个数
        System.out.println("Number of mappings: "+map.size());
        //添加元素
        map.put(Days.Thursday, "4");
        //输出Map
        System.out.println("Map: "+map);
        //输出Map的元素个数
        System.out.println("Number of mappings: "+map.size());
    }
}

输出结果为:

Map: {Monday=1, Tuesday=2, Wednesday=3}
Number of mappings: 3
Map: {Monday=1, Tuesday=2, Wednesday=3, Thursday=4}
Number of mappings: 4

热门文章

优秀文章