1 public static void main(String[] args) { 2 3 Map<Integer, String> map = new HashMap<Integer, String>(); 4 5 map.put(8, "liusan"); 6 7 map.put(2, "liliu"); 8 9 map.put(3, "wuusan"); 10 11 map.put(1, "zhangqi"); 12 13 // 取出map中的所有元素。通过Keyset获取map中所有键的set集合,再通过Set的迭代器获取每一个键,再获取每一个键的值 14 15 Set keySet = map.keySet(); 16 17 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) { 18 Integer key = (Integer) iterator.next(); 19 String value = map.get(key); 20 System.out.println(key + ":" + value); 21 } 22 23 }
调试输出:
1:zhangqi
2:liliu
3:wuusan
8:liusan
时间: 2024-10-13 07:43:50