HashMap 的遍历 for:each 和EntrySet两种方法

有三种方式,EntrySet----forEach----keySet

前2种比较后,keyset实际要遍历2轮.

******EntrySet********

Iterator it = tempMap.entrySet().iterator();
   while (it.hasNext()) {
    Map.Entry entry = (Map.Entry) it.next();
    Object key = entry.getKey();
    Object value = entry.getValue();
    System.out.println("key=" + key + " value=" + value);
}

******EntrySet********

for (Map.Entry entry : tempMap.entrySet()) {
    String key = entry.getKey().toString();
    String value = entry.getValue().toString();
    System.out.println("key=" + key + " value=" + value);
   }

 

posted @ 2013-07-16 16:13  linsen@java  阅读(1046)  评论(0编辑  收藏  举报