通过Map的entrySet方法。将返回一个set集合。然后遍历这个set集合:
package com.howlaa.day04;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class GenericTest {
public static void main(String[] args) {
HashMap<String,Integer> maps = new HashMap<String,Integer>();
maps.put("zhang", 20);
maps.put("hui", 22);
Set<Map.Entry<String,Integer>> entrySet = maps.entrySet();
for (Map.Entry<String, Integer> entry : entrySet) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}