【三】Map的遍历
- package list;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Map.Entry;
- /**
- * @author 小媛
- * Map的遍历
- */
- public class MapPrint {
- public static void main(String[] args) {
- Map<Integer, String> map = new HashMap<Integer, String>();
- map.put(1, "QQ");
- map.put(2, "VV");
- map.put(3, "AA");
- map.put(6, "PP");
- map.put(7, "HH");
- map.put(8, "KK");
- MapPrint2(map);
- }
- /**
- * 通过Entry集合 增强for遍历获取 键-值
- * @param map
- */
- public static void MapPrint1(Map<Integer, String> map) {
- for (Entry<Integer, String> entry : map.entrySet()) {
- System.out.println(entry);
- }
- }
- /**
- * 通过获取Key集合,增强for遍历获取 键-值
- * @param map
- */
- public static void MapPrint2(Map<Integer, String> map) {
- for (Integer key : map.keySet()) {
- System.out.println(key + " = " + map.get(key));
- }
- }
- /**
- * 直接获取Value集合,增强for遍历获取 值
- * @param map
- */
- public static void MapPrint3(Map<Integer, String> map) {
- for(String value : map.values()){
- System.out.println(value);
- }
- }
- }
复制代码 |