本帖最后由 贠(yun)靖 于 2012-3-20 21:13 编辑
给你个例子 遍历的时候推荐使用for循环遍历 节省内存资源, while循环比for循环浪费内存资源
public static void main(String[] args)
{
Map<String,String> map = new HashMap<String,String>();
map.put("1","xiaoming---1");
map.put("2","xiaoming---2");
map.put("3","xiaoming---3");
map.put("4","xiaoming---4");
sop("第一种方式");
Set<String> keyset = map.keySet();
for (Iterator<String> it = keyset.iterator();it.hasNext() ; )
{
String key = it.next();
String value = map.get(key);
sop("key="+key+",value="+value);
}
sop("第二种方式");
Set<Map.Entry<String,String>> entryset = map.entrySet();
for (Iterator<Map.Entry<String,String>> it = entryset.iterator();it.hasNext() ; )
{
Map.Entry<String,String> me = it.next();
String key = me.getKey();
String value = me.getValue();
sop("key="+key+",value="+value);
}
}
public static void sop(Object obj)
{
System.out.println(obj);
}
|