【转】keySet和entrySet的区别

keySet是键的集合,Set里面的类型即key的类型
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);

}

posted @ 2015-09-04 17:15  小东瓜刨冰  阅读(765)  评论(0编辑  收藏  举报