HashMap的遍历有两种常用的方法,那就是使用keyset及entryset来进行遍历,在用keySet(key)取value时候,需要key
第一种:
Map map = new HashMap();
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object value = entry.getValue();
}
第二种:
Map map = new HashMap();
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
Object key = iter.next();
Object value = map.get(key);
}
第二中也可以用for循环 :
Map< String, String> map = new HashMap<String, String>();
for (String key : map.keySet()) {
System.out.println("key= "+key+" and value= "+map.get(key));
}