public class Demo3 { /** * @param args */ public static void main(String[] args) { testMap(); } public static void testMap(){ Map map = new HashMap(); map.put("a", "1"); map.put("b", "2"); map.put("c", "3"); //传统做法 Set set = map.entrySet(); Iterator it = set.iterator(); while(it.hasNext()){ Map.Entry me = (Entry) it.next(); //System.out.println(me.getKey() + "=" + me.getValue()); } //增强for Set set1 = map.entrySet(); for(Object obj : set1){ Map.Entry me = (Entry) obj; System.out.println(me.getKey() + "=" + me.getValue()); } } public void testCollection(){ List list = new ArrayList(); list.add(1); list.add(2); //传统做法 Iterator it = list.iterator(); while(it.hasNext()){ int k = (Integer)it.next(); } //增强for for(Object i : list){ int k = (Integer)i; System.out.println(k); } } public void testArray(){ int arr[] = {1,2,3}; //传统做法 for(int i=0;i<arr.length;i++){ System.out.println(arr[i]); } //增强for要注意的问题:它不能修改数组 for(int i : arr){ i = 10; } //增强for for(int i : arr){ System.out.println(i); } } } 每一个Set对象里面都装了一个Map.Entry对象。