Map集合的四种遍历方式
Map<Integer,String> map = new HashMap<Integer,String>(); for (int i = 0; i < 10000; i++ ){ map.put(i, "thanks"); } /*第一种遍历*/ Iterator<Map.Entry<Integer,String>> iter = map.entrySet().iterator(); while (iter.hasNext()) { Map.Entry<Integer,String> entry = (Map.Entry<Integer,String>) iter.next(); int key = entry.getKey(); String val = entry.getValue(); System.out.println(key + "=" + val + " "); } /*第二种遍历*/ for (Map.Entry<Integer, String> entry : map.entrySet()) { int key = entry.getKey(); String val = entry.getValue(); System.out.println(key + "=" + val + " "); } /*第三种遍历*/ Iterator<Integer> itr = map.keySet().iterator(); while (itr.hasNext()) { int key = itr.next(); String val = map.get(key); System.out.println(key + "=" + val + " "); } /*第四种遍历*/ for (Integer keyset : map.keySet()) { int key = keyset.intValue(); String val = map.get(key); System.out.println(key + "=" + val + " "); }
注:本文非原创