1. 程式人生 > >遍歷Map的兩種方法:keySet()和entrySet()

遍歷Map的兩種方法:keySet()和entrySet()

Map map=new HashMap();

Iterator it=map.keySet().iterator();

Object key;

Object value;

while(it.hasNext()){

key=it.next();

value=map.get(key);

System.out.println(key+":"+value);

}

2.entrySet()

Map map=new HashMap();

Iterator it=map.entrySet().iterator();

Object key;

Object value;

while(it.hasNext()){

Map.Entry entry = (Map.Entry)it.next();

key=entry.getKey();

value=entry.getValue();

System.out.println(key+"="+value);

}