Java EnumMap keySet()
keySet() 获取EnumMap的所有key的Set集合。
1 语法
public Set<K> keySet()
2 参数
无
3 返回值
返回EnumMap的所有key的Set集合。
4 EnumMap keySet()示例1
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* Java EnumMap.keySet的例子
*/
import java.util.*;
public class Demo {
// create an enum
public enum Tutorial {
CSS, Python, PHP, Java, Javascript
};
public static void main(String[] args) {
//创建EnumMap
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);
//获取所有key
Set<Tutorial> set = map.keySet();
//输出Map
System.out.println("KeySet: "+set);
}
}
输出结果为:
Map: {CSS=1, Python=2, PHP=3, Java=4}
KeySet: [CSS, Python, PHP, Java]
5 EnumMap keySet()示例2
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* Java EnumMap.keySet的例子
*/
import java.util.*;
public class Demo {
//创建枚举
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
System.out.println("Map: "+map);
//获取key
Set<Days> set = map.keySet();
//输出Map
System.out.println("KeySet: "+set);
}
}
输出结果为:
Map: {Monday=1, Tuesday=2, Wednesday=3}
KeySet: [Monday, Tuesday, Wednesday]
热门文章
优秀文章