public static void main(String[] args) { Map<Integer, String> map = new HashMap<Integer, String>(); map.put(8, "liusan"); map.put(2, "liliu"); map.put(3, "wuusan"); map.put(1, "zhangqi"); // 取出map中的所有元素。通过Keyset获取map中所有键的set集合,再通过Set的迭代器获取每一个键,再获取每一个键的值 Set keySet = map.keySet(); for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
Integer key = (Integer) iterator.next();
String value = map.get(key);
System.out.println(key + ":" + value);
} }
调试输出:
1:zhangqi
2:liliu
3:wuusan
8:liusan
2:liliu
3:wuusan
8:liusan