方式一 . entrySet()
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "me");
map.put(2, "you");
Set<Map.Entry<Integer, String>> entries = map.entrySet();
for (Map.Entry<Integer,String> entry : entries) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
方式二. keySet()
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "me");
map.put(2, "you");
Set<Integer> integers = map.keySet();
for (int key: integers) {
System.out.println(key + "=" +map.get(key));
}
// 单取值
for (Object value : map.values()) {
System.out.println(value);
}
方式三. iterator()
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "me");
map.put(2, "you");
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, String> next = iterator.next();
System.out.println(next.getKey() + next.getValue());
}