public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("Tom", "CoreJava");
map.put("John", "Oracle");
map.put("Susan", "Oracle");
map.put("Jerry", "JDBC");
map.put("Jim", "Unix");
map.put("Kevin", "JSP");
map.put("Lucy", "JSP");
// 增加一个新老师
map.put("Allen", "JDBC");
// Lucy教CoreJava
map.put("Lucy", "CoreJava");
// 遍历Map,输出所有的老师及老师教授的课程
"/**
* keySet()返回此映射关系中包含键的Set视图
*/"
Set<String> keys = map.keySet();
for (String key : keys) {
System.out.println(key + "教" + map.get(key));
}
'/**
* 结果:
* Jerry教JDBC
* Jim教Unix
* Tom教CoreJava
* Susan教Oracle
* Lucy教CoreJava
* John教Oracle
* Allen教JDBC
* Kevin教JSP
*/'
'/**
* entrySet()方法:返回此映射中包含的映射关系的Set视图
*/'
System.out.println("***************************");
Set<Map.Entry<String, String>> keysNew = map.entrySet();
for (Map.Entry<String, String> key : keysNew) {
// System.out.println(map.get(key)); //得到的是null
System.out.println(key);
/**
* 结果:
* Jerry=JDBC
* Jim=Unix
* Tom=CoreJava
* Susan=Oracle
* Lucy=CoreJava
* John=Oracle
* Allen=JDBC
* Kevin=JSP
*
*/
}
}
网友评论