1 Mapmap = new HashMap (); 2 map.put(1, "a"); 3 map.put(2, "b"); 4 5 System.out.println("(1):"); 6 //1.Map输出1(只遍历一次,速度快) 7 Iterator itr11 = map.entrySet().iterator(); 8 while(itr11.hasNext()){ 9 Map.Entry e = (Map.Entry)itr11.next();10 System.out.println(e.getKey()+"==="+e.getValue());11 }12 System.out.println();13 System.out.println("(2):");14 //2.Map输出2(速度慢)15 Set set = map.keySet();//用接口实例接口16 Iterator itr22 = set.iterator();17 while(itr22.hasNext()){ //遍历二次,速度慢18 Integer key = (Integer)itr22.next();19 System.out.println(key+"==="+map.get(key));20 }21 //3.Map输出3(for语句)22 System.out.println();23 System.out.println("(3):");24 Set set00 = map.keySet();25 for(Integer s :set00){26 System.out.println(s+"==="+map.get(s));27 }28 29 //4.Map输出430 System.out.println();31 System.out.println("(4):");32 Iterator itr44 = map.values().iterator(); 33 while(itr44.hasNext()){34 System.out.println(itr44.next());35 }36 System.out.println();37 38 //5.Map输出539 System.out.println("(5):");40 System.out.println(map);41 42 //6.Map输出643 System.out.println();44 System.out.println("(6):");45 for(Map.Entry en:map.entrySet()){46 System.out.println(en.getKey()+"==="+en.getValue());47 }
输出: