方法多种多样,其实大差不差~
- 通过 键值集(entrySet)遍历
System.out.println("Ⅰ、使用 entrySet() 方法遍历:");
for (Map.Entry<String, String> entry : map.entrySet()) {String key = entry.getKey();String value = entry.getValue();System.out.println("Key: " + key + ", Value: " + value);
}
- 通过 键集(keySet)遍历
System.out.println("----------------------------");
System.out.println("Ⅱ、使用 keySet() 方法遍历:");
for (String key : map.keySet()) {String value = map.get(key);System.out.println("Key: " + key + ", Value: " + value);
}
- 通过 值集(values),只能获取到值,不能获取到键
System.out.println("----------------------------");
System.out.println("Ⅲ、也可以使用 values() 方法遍历所有值,但这种方式无法获取对应的键:");
for (String value : map.values()) {System.out.println("Value: " + value);
}
- EntrySet迭代器的遍历
System.out.println("----------------------------");
System.out.println("EntrySet迭代器的遍历,在IDEA中会推荐替换while为增强for");
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {Map.Entry<String, String> entry = iterator.next();System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
- KeySet迭代器的遍历
System.out.println("----------------------------");
System.out.println("Ⅴ、KeySet迭代器的遍历,在IDEA中会推荐替换while为增强for");
Iterator<String> iterator2 = map.keySet().iterator();
while (iterator2.hasNext()) {String key = iterator2.next();System.out.println("Key: " + key + ", Value: " + map.get(key));
}
- StreamAPI
System.out.println("----------------------------");
System.out.println("Ⅵ、StreamAPI,IDEA会推荐替换stream().forEach()为map.entrySet().forEach()");
System.out.println("IDEA会推荐替换map.entrySet().forEach()为map.forEach(),见最后一个");
- EntrySet的forEach()
System.out.println("----------------------------");
System.out.println("Ⅶ、EntrySet的forEach()");
map.entrySet().forEach(entry -> System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()));
- Java 8引入的Map接口中的forEach()方法
System.out.println("----------------------------");
System.out.println("Ⅷ、Java 8引入的Map接口中的forEach()方法");
System.out.println("简介且易阅读");
map.forEach((key, value) -> System.out.println("Key: " + key + ", Value: " + value));
测试代码
public class Main {public static void main(String[] args) {Map<String, String> map = new HashMap<>();map.put("1", "111");map.put("2", "222");map.put("3", "test");System.out.println("Ⅰ、使用 entrySet() 方法遍历:");for (Map.Entry<String, String> entry : map.entrySet()) {String key = entry.getKey();String value = entry.getValue();System.out.println("Key: " + key + ", Value: " + value);}System.out.println("----------------------------");System.out.println("Ⅱ、使用 keySet() 方法遍历:");for (String key : map.keySet()) {String value = map.get(key);System.out.println("Key: " + key + ", Value: " + value);}System.out.println("----------------------------");System.out.println("Ⅲ、也可以使用 values() 方法遍历所有值,但这种方式无法获取对应的键:");for (String value : map.values()) {System.out.println("Value: " + value);}System.out.println("----------------------------");System.out.println("EntrySet迭代器的遍历,在IDEA中会推荐替换while为增强for");Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();while (iterator.hasNext()) {Map.Entry<String, String> entry = iterator.next();System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());}System.out.println("----------------------------");System.out.println("KeySet迭代器的遍历,在IDEA中会推荐替换while为增强for");Iterator<String> iterator2 = map.keySet().iterator();while (iterator2.hasNext()) {String key = iterator2.next();System.out.println("Key: " + key + ", Value: " + map.get(key));}System.out.println("----------------------------");System.out.println("values迭代器的遍历,在IDEA中会推荐替换while为增强for");Iterator<String> iterator3 = map.values().iterator();while (iterator3.hasNext()) {String value = iterator3.next();System.out.println("Value: " + value);}System.out.println("----------------------------");System.out.println("StreamAPI,IDEA会推荐替换stream().forEach()为map.entrySet().forEach(),见倒二");System.out.println("IDEA会推荐替换map.entrySet().forEach()为map.forEach(),见最后一个");map.entrySet().stream().forEach(entry -> System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()));System.out.println("----------------------------");System.out.println("EntrySet的forEach()");map.entrySet().forEach(entry -> System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()));System.out.println("----------------------------");System.out.println("Java 8引入的Map接口中的forEach()方法");System.out.println("简介且易阅读");map.forEach((key, value) -> System.out.println("Key: " + key + ", Value: " + value));}}