Java基础篇 Map循环的方式
(一)循环例子
Mapmap = new HashMap (); map.put("pen","thank you for attention"); map.put("lao","thank you for attention"); map.put("xi","thank you for attention");
(二)通过key Set key = map.keySet()
for(String key:map.keySet()){ System.out.println("key="+key+" value="+map.get(key)); }
(三)key和value都循环
for(Map.Entryentry : map.entrySet()){ System.out.println("key="+entry.getKey()+" value="+entry.getValue()); }
(四)迭代器
Iterator> iterator = map.entrySet().iterator(); while (iterator.hasNext()){ Map.Entry entry = iterator.next(); System.out.println("key="+entry.getKey()+" value="+entry.getValue()); }
(五)仅循环value
for(String value : map.values()){ System.out.println("value="+value); }