遍历并打印出Map各元素的key和value
拿个小本本积累知识点啦!
一、使用hm.keySet()
package com.gec.lalala;
import java.util.HashMap;
import java.util.Map;
public class MapTest {
public static void main(String[] args) {
// key为String,value为Object,可放对象等……
Map<String, Object> map = new HashMap<String, Object>();
map.put("1", "小白1");
map.put("2", "小白2");
map.put("3", "小白3");
map.put("4", "小白4");
map.put("5", "小白5");
System.err.println("map.keySet() : ");
for (String mp : map.keySet()) {
System.out.println("key = " + mp + ", value = " + map.get(mp));
}
}
}
二、使用使用hm.entrySet()
package com.gec.lalala;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class MapTest {
public static void main(String[] args) {
// key为String,value为Object,可放对象等……
Map<String, Object> map = new HashMap<String, Object>();
map.put("1", "小白1");
map.put("2", "小白2");
map.put("3", "小白3");
map.put("4", "小白4");
map.put("5", "小白5");
System.err.println("map.entrySet() : ");
for (Entry<String, Object> en : map.entrySet()) {
System.out.println("Key = " + en.getKey() + ", Value = " + en.getValue());
}
}
}