Java的Map可以使用以下几种方式进行遍历:
使用entrySet()方法遍历:通过Map的entrySet()方法获取到所有的键值对对象,然后使用迭代器或者增强for循环遍历。Map<String, Integer> map = new HashMap<>();// 添加键值对map.put("A", 1);map.put("B", 2);// 遍历for (Map.Entry<String, Integer> entry : map.entrySet()) {String key = entry.getKey();Integer value = entry.getValue();// 打印键值对System.out.println(key + " : " + value);}使用keySet()方法遍历:通过Map的keySet()方法获取到所有的键集合,然后使用迭代器或者增强for循环遍历,再通过获取value的方式获取对应的值。Map<String, Integer> map = new HashMap<>();// 添加键值对map.put("A", 1);map.put("B", 2);// 遍历for (String key : map.keySet()) {Integer value = map.get(key);// 打印键值对System.out.println(key + " : " + value);}使用values()方法遍历:通过Map的values()方法获取到所有的值集合,然后使用迭代器或者增强for循环遍历。Map<String, Integer> map = new HashMap<>();// 添加键值对map.put("A", 1);map.put("B", 2);// 遍历for (Integer value : map.values()) {// 打印值System.out.println(value);}使用Lambda表达式遍历(Java 8及以上版本):使用Java 8的新特性Lambda表达式对Map进行遍历。Map<String, Integer> map = new HashMap<>();// 添加键值对map.put("A", 1);map.put("B", 2);// 遍历map.forEach((key, value) -> {// 打印键值对System.out.println(key + " : " + value);}); 
