Java EnumMap get()
get() 用于获取指定key的映射值。
1 语法
public V get(Object key)
2 参数
key:需要查询的键。
3 返回值
返回指定键映射到的值。
4 EnumMap get()示例1
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* Java EnumMap.get的例子
*/
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);
// 查询Tutorial.CSS
String value = map.get(Tutorial.CSS);
System.out.println("Tutorial.CSS value: "+value);
System.out.println("Tutorial.Javascript value: "+ map.get(Tutorial.Javascript));
}
}
输出结果为:
Map: {CSS=1, Python=2, PHP=3, Java=4}
Tutorial.CSS value: 1
Tutorial.Javascript value: null
5 EnumMap get()示例2
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* Java EnumMap.get的例子
*/
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);
// 查询Days.Monday
String value = map.get(Days.Monday);
System.out.println("Days.Monday value: "+value);
System.out.println("Days.Thursday value: "+map.get(Days.Thursday));
}
}
输出结果为:
Map: {Monday=1, Tuesday=2, Wednesday=3}
Days.Monday value: 1
Days.Thursday value: null
热门文章
优秀文章