以下是兩種方法遍歷HashMap的代碼示例
方法
Map map = new HashMap(); mapput(a ); mapput(b ); mapput(c ); mapput(d ); mapput(e ); for (Iterator iter = mapkeySet(erator(); iterhasNext();) { Object key = iternext(); Object val = mapget(key); Systemoutprintln(key:+key); Systemoutprintln(value:+val); }
方法
view sourceprint?Map map = new HashMap();
mapput(a );
mapput(b );
mapput(c );
mapput(d );
mapput(e );
for (Iterator iter = mapentrySet(erator(); iterhasNext();) {
MapEntry entry = (MapEntry) iternext();
Object key = entrygetKey();
Object val = entrygetValue();
Systemoutprintln(key:+key);
Systemoutprintln(value:+val);
}
view sourceprint?<%
Map<String Integer> map = (Map<String Integer>) sessiongetAttribute(UserInfo);
outprintln(map == null);
if (map == null)
{
map = new HashMap<String Integer>();
outprintln(map == null);
mapput(steden );
mapput(steden );
mapput(steden );
mapput(steden );
}
sessionsetAttribute(UserInfo map);
for(MapEntry<String Integer> item : mapentrySet())
{
outprintln(itemgetKey());
outprintln(itemgetValue());
outprintln(<br />);
}
%>
性能從低到高
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27112.html