复习map的过程中想到的,做个简单的记录
1 public class HashMapTest { 2 3 public static void main(String args[]) { 4 Map<Integer, Integer> hm = new HashMap<Integer, Integer>(); 5 hm.put(1, 8); 6 hm.put(2, 7); 7 hm.put(3, 6); 8 hm.put(4, 5); 9 System.out.println(hm); 10 System.out.println("第一种:foreach循环"); 11 for (Integer i : hm.keySet()) { 12 Integer a = hm.get(i); 13 System.out.println(a); 14 } 15 16 System.out.println("第二种:迭代器"); 17 Iterator<Map.Entry<Integer, Integer>> it = hm.entrySet().iterator(); 18 while (it.hasNext()) { 19 System.out.println(it.next().getValue()); 20 } 21 22 System.out.println("第三种:"); 23 for (Map.Entry<Integer, Integer> entry : hm.entrySet()) { 24 System.out.println(entry.getKey() + "--" + entry.getValue()); 25 } 26 27 } 28 29 }
时间: 2024-10-11 05:39:39