转自http://rain-2372.iteye.com/blog/1615615
package com.spring.test_B11_aop1; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set; public class MapTest { public static void main(String[] args) {
// java中遍历MAP的几种方法
// Java代码
Map<String, String> map = new HashMap<String, String>();
map.put("username", "qq");
map.put("passWord", "123");
map.put("userID", "1");
map.put("email", "qq@qq.com"); // 第一种用for循环
// Java代码
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + "--->" + entry.getValue());
} // 第二种用迭代
// Java代码
Set<?> set = map.entrySet();
Iterator<?> i = set.iterator();
while (i.hasNext()) {
@SuppressWarnings("unchecked")
Map.Entry<String, String> entry1 = (Map.Entry<String, String>) i.next();
System.out.println(entry1.getKey() + "==" + entry1.getValue());
} // 用keySet()迭代
// Java代码
Iterator<String> it = map.keySet().iterator();
while (it.hasNext()) {
String key;
String value;
key = it.next().toString();
value = map.get(key);
System.out.println(key + "--" + value);
} // 用entrySet()迭代
// Java代码
Iterator<?> it2 = map.entrySet().iterator();
System.out.println(map.entrySet().size());
String key;
String value;
while (it2.hasNext()) {
@SuppressWarnings("rawtypes")
Map.Entry entry = (Map.Entry) it2.next();
key = entry.getKey().toString();
value = entry.getValue().toString();
System.out.println(key + "====" + value);
}
}
}