public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("key01", "value01"); map.put("key02", "value02"); map.put("key03", "value03"); //方法一:二次取值(普遍使用) System.out.println("方法一:通过map.keyset()获取所有的key;"); for (String key : map.keySet()) { System.out.println(key + ":" + map.get(key) ); } //方法二: System.out.println("方法二:通过Map.entrySet(),使用iterator遍历key;"); Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator(); while(iterator.hasNext()) { Map.Entry<String, String> entry = iterator.next(); System.out.println(entry.getKey() + ":" + entry.getValue() ); } //方法三:(推荐使用,尤其是大容量时) System.out.println("方法三:通过Map.entrySet()遍历key;"); Set<Entry<String,String>> set = map.entrySet(); for (Entry<String, String> entry : set) { System.out.println(entry.getKey() + ":" + entry.getValue() ); } //方法四:缺点-不能遍历key System.out.println("方法三:通过Map.values()得到所有的value;"); Collection<String> values = map.values(); for (String value : values) { System.out.println("value:" + value); } }