Map的迭代

 public static void main(String[] args) {

         Map<String, Integer> map = new HashMap<String, Integer>();
map.put("张三", 12);
map.put("张三1", 121);
map.put("张三2", 122);
map.put("张三3", 123);
map.put("张三4", 124);
map.put("张三5", 125);
map.put("张三6", 126);
map.put("张三7", 127); // keySet
Set<String> set = map.keySet();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String key = it.next();
System.out.println(key + "---" + map.get(key));
}
System.out.println("==============================================");
// foreach 遍历keySet
for (String string : map.keySet()) {
System.out.println(string + "===" + map.get(string));
} System.out.println("=============================================="); // entrySet
Set<Entry<String, Integer>> setenty = map.entrySet();
Iterator<Entry<String, Integer>> ite = setenty.iterator();
while (ite.hasNext()) {
Entry<String, Integer> m = ite.next();
System.out.println(m.getKey() + "---" + m.getValue());
} System.out.println("=============================================="); // foreach 遍历entrySet
for (Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + "===" + entry.getValue());
}
}
上一篇:java获取MP3的播放长度


下一篇:异常:java.lang.IllegalStateException: No instances found of configserver(里面是一个微服务名)