【转】keySet和entrySet的区别
keySet是键的集合,Set里面的类型即key的类型
entrySet是 键-值 对的集合,Set里面的类型是Map.Entry
entrySet是 键-值 对的集合,Set里面的类型是Map.Entry
1.keySet()
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);
}