java遍历hashMap、hashSet、Hashtable
一.遍历HashMap Map<Integer, String> map = new HashMap<Integer, String>(); 方法一:效率高 for(Entry<Integer, String> entry:map.entrySet()){ System.out.println(entry.getKey()+"="+entry.getValue()); } 方法二: for (Integer key : map.keySet()) { String value = map.get(key); (这样取value会导致效率低,不建议。) System.out.println(value); } 还可以单独遍历value for (Integer value : map.values()) { System.out.println("Value = " + value); } 方法三:(好处:可以在遍历时调用iterator.remove()来删除entries) Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator(); while (entries.hasNext()) { Map.Entry<Integer, Integer> entry =