方法1:
Map map = new HashMap(); map.put("a", "1"); map.put("b", "2"); map.put("c", "3"); map.put("d", "4"); map.put("e", "5"); for (Iterator iter = map.keySet().iterator(); iter.hasNext();) { Object key = iter.next(); Object val = map.get(key); System.out.println("key:"+key); System.out.println("value:"+val); }
方法2:
view sourceprint?Map map = new HashMap();
map.put("a", "1");
map.put("b", "2");
map.put("c", "3");
map.put("d", "4");
map.put("e", "5");
for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
System.out.println("key:"+key);
System.out.println("value:"+val);
}
view sourceprint?<%
Map<String, Integer> map = (Map<String, Integer>) session.getAttribute("UserInfo");
out.println(map == null);
if (map == null)
{
map = new HashMap<String, Integer>();
out.println(map == null);
map.put("steden", 0);
map.put("steden1", 0);
map.put("steden2", 0);
map.put("steden3", 0);
}
session.setAttribute("UserInfo", map);
for(Map.Entry<String, Integer> item : map.entrySet())
{
out.println(item.getKey());
out.println(item.getValue());
out.println("<br />");
}
%>
性能从低到高。。。